Contiki-NG
Loading...
Searching...
No Matches
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#define _GNU_SOURCE /* For vasprintf. */
32#include <stdio.h>
33#include <stdarg.h>
34#include <stdlib.h>
35#include <string.h>
36
37#ifndef MAX_LOG_LENGTH
38#define MAX_LOG_LENGTH 8192
39#endif /* MAX_LOG_LENGTH */
40
41#ifndef COOJA_LOG_WITH_SLIP
42#define COOJA_LOG_WITH_SLIP 0
43#endif /* COOJA_LOG_WITH_SLIP */
44
45/* Variables shared between COOJA and Contiki */
46char simLoggedData[MAX_LOG_LENGTH];
47int simLoggedLength;
48char simLoggedFlag;
49
50/*-----------------------------------------------------------------------------------*/
51int
52simlog_char(char c)
53{
54 if (simLoggedLength + 1 > MAX_LOG_LENGTH) {
55 /* Dropping message due to buffer overflow */
56 return EOF;
57 }
58
59 simLoggedData[simLoggedLength] = c;
60 simLoggedLength += 1;
61 simLoggedFlag = 1;
62 return c;
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 int log_putchar_with_slip = COOJA_LOG_WITH_SLIP != 0;
87void
88log_set_putchar_with_slip(int with)
89{
90 log_putchar_with_slip = with;
91}
92/*-----------------------------------------------------------------------------------*/
93int
95{
96#define SLIP_END 0300
97 static char debug_frame = 0;
98
99 if(log_putchar_with_slip) {
100 if(!debug_frame) { /* Start of debug output */
101 simlog_char(SLIP_END);
102 simlog_char('\r'); /* Type debug line == '\r' */
103 debug_frame = 1;
104 }
105
106 simlog_char((char)c);
107
108 /*
109 * Line buffered output, a newline marks the end of debug output and
110 * implicitly flushes debug output.
111 */
112 if(c == '\n') {
113 simlog_char(SLIP_END);
114 debug_frame = 0;
115 }
116 } else {
117 simlog_char(c);
118 }
119 return c;
120}
121/*-----------------------------------------------------------------------------------*/
122#ifndef __APPLE__
123extern int __wrap_putchar(int c) __attribute__((alias("putchar")));
124extern int __wrap_puts(const char *str) __attribute__((nonnull, alias("puts")));
125extern int __wrap_printf(const char *fmt, ...) __attribute__((nonnull, alias("printf")));
126#endif
127/*---------------------------------------------------------------------------*/
128int
129putchar(int c)
130{
131 return simlog_char(c);
132}
133/*---------------------------------------------------------------------------*/
134int
135puts(const char* s)
136{
137 simlog(s);
138 return simlog_char('\n');
139}
140/*---------------------------------------------------------------------------*/
141int
142printf(const char *fmt, ...)
143{
144 va_list ap;
145 va_start(ap, fmt);
146 char *buf;
147 int res = vasprintf(&buf, fmt, ap);
148 va_end(ap);
149 if(res > 0) {
150 simlog(buf);
151 free(buf);
152 }
153 return res;
154}
int dbg_putchar(int c)
Print a character to debug output.
Definition cooja-log.c:94