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 #ifndef MAX_LOG_LENGTH
37 #define MAX_LOG_LENGTH 8192
38 #endif /* MAX_LOG_LENGTH */
39 
40 const struct simInterface simlog_interface;
41 
42 /* Variables shared between COOJA and Contiki */
43 char simLoggedData[MAX_LOG_LENGTH];
44 int simLoggedLength;
45 char simLoggedFlag;
46 
47 /*-----------------------------------------------------------------------------------*/
48 void
49 simlog_char(char c)
50 {
51  if (simLoggedLength + 1 > MAX_LOG_LENGTH) {
52  /* Dropping message due to buffer overflow */
53  return;
54  }
55 
56  simLoggedData[simLoggedLength] = c;
57  simLoggedLength += 1;
58  simLoggedFlag = 1;
59 }
60 /*-----------------------------------------------------------------------------------*/
61 void
62 simlog(const char *message)
63 {
64  int message_len = strlen(message);
65  if(simLoggedLength + message_len > MAX_LOG_LENGTH) {
66  /* Dropping message due to buffer overflow */
67  return;
68  }
69 
70  memcpy(simLoggedData + simLoggedLength, message, message_len);
71  simLoggedLength += message_len;
72  simLoggedFlag = 1;
73 }
74 /*-----------------------------------------------------------------------------------*/
75 void
76 log_message(const char *part1, const char *part2)
77 {
78  simlog(part1);
79  simlog(part2);
80 }
81 /*-----------------------------------------------------------------------------------*/
82 static void
83 doInterfaceActionsBeforeTick(void)
84 {
85 }
86 /*-----------------------------------------------------------------------------------*/
87 static void
88 doInterfaceActionsAfterTick(void)
89 {
90 }
91 /*-----------------------------------------------------------------------------------*/
92 static int log_putchar_with_slip;
93 void
94 log_set_putchar_with_slip(int with)
95 {
96  log_putchar_with_slip = with;
97 }
98 /*-----------------------------------------------------------------------------------*/
99 int
101 {
102 #define SLIP_END 0300
103  static char debug_frame = 0;
104 
105  if(log_putchar_with_slip) {
106  simlog_char(SLIP_END);
107 
108  if(!debug_frame) { /* Start of debug output */
109  simlog_char(SLIP_END);
110  simlog_char('\r'); /* Type debug line == '\r' */
111  debug_frame = 1;
112  }
113 
114  simlog_char((char)c);
115 
116  /*
117  * Line buffered output, a newline marks the end of debug output and
118  * implicitly flushes debug output.
119  */
120  if(c == '\n') {
121  simlog_char(SLIP_END);
122  debug_frame = 0;
123  }
124  } else {
125  simlog_char(c);
126  }
127  return c;
128 }
129 /*-----------------------------------------------------------------------------------*/
130 unsigned int
131 dbg_send_bytes(const unsigned char *s, unsigned int len)
132 {
133  unsigned int i;
134  for(i = 0; i < len && s && *s != 0; i++) {
135  putchar(*s++);
136  }
137  return i;
138 }
139 /*-----------------------------------------------------------------------------------*/
140 
141 SIM_INTERFACE(simlog_interface,
142  doInterfaceActionsBeforeTick,
143  doInterfaceActionsAfterTick);
unsigned int dbg_send_bytes(const unsigned char *s, unsigned int len)
Print a stream of bytes.
Definition: cooja-log.c:131
int dbg_putchar(int c)
Print a character to debug output.
Definition: cooja-log.c:100