Contiki 3.x
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 "sys/log.h"
35 #include "lib/simEnvChange.h"
36 
37 #define IMPLEMENT_PRINTF 1
38 
39 #if WITH_UIP
40 /* uIP packets via SLIP */
41 #include "uip.h"
42 #define MAX_LOG_LENGTH (2*UIP_BUFSIZE)
43 #else /* WITH_UIP */
44 #define MAX_LOG_LENGTH 1024
45 #endif /* WITH_UIP */
46 
47 #if MAX_LOG_LENGTH < 1024
48 #undef MAX_LOG_LENGTH
49 #define MAX_LOG_LENGTH 1024
50 #endif /* MAX_LOG_LENGTH < 1024 */
51 
52 
53 const struct simInterface simlog_interface;
54 
55 /* Variables shared between COOJA and Contiki */
56 char simLoggedData[MAX_LOG_LENGTH];
57 int simLoggedLength;
58 char simLoggedFlag;
59 
60 /*-----------------------------------------------------------------------------------*/
61 void
62 simlog_char(char c)
63 {
64  if (simLoggedLength + 1 > MAX_LOG_LENGTH) {
65  /* Dropping message due to buffer overflow */
66  return;
67  }
68 
69  simLoggedData[simLoggedLength] = c;
70  simLoggedLength += 1;
71  simLoggedFlag = 1;
72 }
73 /*-----------------------------------------------------------------------------------*/
74 void
75 simlog(const char *message)
76 {
77  if (simLoggedLength + strlen(message) > MAX_LOG_LENGTH) {
78  /* Dropping message due to buffer overflow */
79  return;
80  }
81 
82  memcpy(simLoggedData + simLoggedLength, message, strlen(message));
83  simLoggedLength += strlen(message);
84  simLoggedFlag = 1;
85 }
86 /*-----------------------------------------------------------------------------------*/
87 void
88 log_message(const char *part1, const char *part2)
89 {
90  simlog(part1);
91  simlog(part2);
92 }
93 /*-----------------------------------------------------------------------------------*/
94 static void
95 doInterfaceActionsBeforeTick(void)
96 {
97 }
98 /*-----------------------------------------------------------------------------------*/
99 static void
100 doInterfaceActionsAfterTick(void)
101 {
102 }
103 /*-----------------------------------------------------------------------------------*/
104 static int log_putchar_with_slip;
105 void
106 log_set_putchar_with_slip(int with)
107 {
108  log_putchar_with_slip = with;
109 }
110 /*-----------------------------------------------------------------------------------*/
111 #if IMPLEMENT_PRINTF
112 int
113 putchar(int c)
114 {
115 #define SLIP_END 0300
116  static char debug_frame = 0;
117 
118  if(log_putchar_with_slip) {
119  simlog_char(SLIP_END);
120 
121  if(!debug_frame) { /* Start of debug output */
122  simlog_char(SLIP_END);
123  simlog_char('\r'); /* Type debug line == '\r' */
124  debug_frame = 1;
125  }
126 
127  simlog_char((char)c);
128 
129  /*
130  * Line buffered output, a newline marks the end of debug output and
131  * implicitly flushes debug output.
132  */
133  if(c == '\n') {
134  simlog_char(SLIP_END);
135  debug_frame = 0;
136  }
137 
138  return c;
139  } else {
140  simlog_char(c);
141  return c;
142  }
143 }
144 /*-----------------------------------------------------------------------------------*/
145 int
146 puts(const char* s)
147 {
148  simlog(s);
149  simlog_char('\n');
150  return 0;
151 }
152 /*-----------------------------------------------------------------------------------*/
153 int
154 printf(const char *fmt, ...)
155 {
156  int res;
157  static char buf[MAX_LOG_LENGTH];
158  va_list ap;
159  int i;
160 
161  va_start(ap, fmt);
162  res = vsnprintf(buf, MAX_LOG_LENGTH, fmt, ap);
163  va_end(ap);
164 
165  // simlog(buf);
166  for(i = 0; i < res; i++) {
167  putchar(buf[i]);
168  }
169  return res;
170 }
171 #endif /* IMPLEMENT_PRINTF */
172 /*-----------------------------------------------------------------------------------*/
173 
174 SIM_INTERFACE(simlog_interface,
175  doInterfaceActionsBeforeTick,
176  doInterfaceActionsAfterTick);
Header file for the uIP TCP/IP stack.