Contiki-NG
cooja-log.c
1 /*
2  * Copyright (c) 2006, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include "lib/simEnvChange.h"
35 
36 #define IMPLEMENT_PRINTF 1
37 
38 #ifndef MAX_LOG_LENGTH
39 #define MAX_LOG_LENGTH 8192
40 #endif /* MAX_LOG_LENGTH */
41 
42 
43 const struct simInterface simlog_interface;
44 
45 /* Variables shared between COOJA and Contiki */
46 char simLoggedData[MAX_LOG_LENGTH];
47 int simLoggedLength;
48 char simLoggedFlag;
49 
50 /*-----------------------------------------------------------------------------------*/
51 void
52 simlog_char(char c)
53 {
54  if (simLoggedLength + 1 > MAX_LOG_LENGTH) {
55  /* Dropping message due to buffer overflow */
56  return;
57  }
58 
59  simLoggedData[simLoggedLength] = c;
60  simLoggedLength += 1;
61  simLoggedFlag = 1;
62 }
63 /*-----------------------------------------------------------------------------------*/
64 void
65 simlog(const char *message)
66 {
67  if (simLoggedLength + strlen(message) > MAX_LOG_LENGTH) {
68  /* Dropping message due to buffer overflow */
69  return;
70  }
71 
72  memcpy(simLoggedData + simLoggedLength, message, strlen(message));
73  simLoggedLength += strlen(message);
74  simLoggedFlag = 1;
75 }
76 /*-----------------------------------------------------------------------------------*/
77 void
78 log_message(const char *part1, const char *part2)
79 {
80  simlog(part1);
81  simlog(part2);
82 }
83 /*-----------------------------------------------------------------------------------*/
84 static void
85 doInterfaceActionsBeforeTick(void)
86 {
87 }
88 /*-----------------------------------------------------------------------------------*/
89 static void
90 doInterfaceActionsAfterTick(void)
91 {
92 }
93 /*-----------------------------------------------------------------------------------*/
94 static int log_putchar_with_slip;
95 void
96 log_set_putchar_with_slip(int with)
97 {
98  log_putchar_with_slip = with;
99 }
100 /*-----------------------------------------------------------------------------------*/
101 #if IMPLEMENT_PRINTF
102 int
103 putchar(int c)
104 {
105 #define SLIP_END 0300
106  static char debug_frame = 0;
107 
108  if(log_putchar_with_slip) {
109  simlog_char(SLIP_END);
110 
111  if(!debug_frame) { /* Start of debug output */
112  simlog_char(SLIP_END);
113  simlog_char('\r'); /* Type debug line == '\r' */
114  debug_frame = 1;
115  }
116 
117  simlog_char((char)c);
118 
119  /*
120  * Line buffered output, a newline marks the end of debug output and
121  * implicitly flushes debug output.
122  */
123  if(c == '\n') {
124  simlog_char(SLIP_END);
125  debug_frame = 0;
126  }
127 
128  return c;
129  } else {
130  simlog_char(c);
131  return c;
132  }
133 }
134 /*-----------------------------------------------------------------------------------*/
135 int
136 puts(const char* s)
137 {
138  simlog(s);
139  simlog_char('\n');
140  return 0;
141 }
142 /*-----------------------------------------------------------------------------------*/
143 int
144 printf(const char *fmt, ...)
145 {
146  int res;
147  static char buf[MAX_LOG_LENGTH];
148  va_list ap;
149  int i;
150 
151  va_start(ap, fmt);
152  res = vsnprintf(buf, MAX_LOG_LENGTH, fmt, ap);
153  va_end(ap);
154 
155  // simlog(buf);
156  for(i = 0; i < res; i++) {
157  putchar(buf[i]);
158  }
159  return res;
160 }
161 #endif /* IMPLEMENT_PRINTF */
162 /*-----------------------------------------------------------------------------------*/
163 
164 SIM_INTERFACE(simlog_interface,
165  doInterfaceActionsBeforeTick,
166  doInterfaceActionsAfterTick);