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