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/*-----------------------------------------------------------------------------------*/
79static int log_putchar_with_slip = COOJA_LOG_WITH_SLIP != 0;
80void
81log_set_putchar_with_slip(int with)
82{
83 log_putchar_with_slip = with;
84}
85/*-----------------------------------------------------------------------------------*/
86int
88{
89#define SLIP_END 0300
90 static char debug_frame = 0;
91
92 if(log_putchar_with_slip) {
93 if(!debug_frame) { /* Start of debug output */
94 simlog_char(SLIP_END);
95 simlog_char('\r'); /* Type debug line == '\r' */
96 debug_frame = 1;
97 }
98
99 simlog_char((char)c);
100
101 /*
102 * Line buffered output, a newline marks the end of debug output and
103 * implicitly flushes debug output.
104 */
105 if(c == '\n') {
106 simlog_char(SLIP_END);
107 debug_frame = 0;
108 }
109 } else {
110 simlog_char(c);
111 }
112 return c;
113}
114/*-----------------------------------------------------------------------------------*/
115#ifndef __APPLE__
116extern int __wrap_putchar(int c) __attribute__((alias("putchar")));
117extern int __wrap_puts(const char *str) __attribute__((nonnull, alias("puts")));
118extern int __wrap_printf(const char *fmt, ...) __attribute__((nonnull, alias("printf")));
119#endif
120/*---------------------------------------------------------------------------*/
121int
122putchar(int c)
123{
124 return simlog_char(c);
125}
126/*---------------------------------------------------------------------------*/
127int
128puts(const char* s)
129{
130 simlog(s);
131 return simlog_char('\n');
132}
133/*---------------------------------------------------------------------------*/
134int
135printf(const char *fmt, ...)
136{
137 va_list ap;
138 va_start(ap, fmt);
139 char *buf;
140 int res = vasprintf(&buf, fmt, ap);
141 va_end(ap);
142 if(res > 0) {
143 simlog(buf);
144 free(buf);
145 }
146 return res;
147}
int dbg_putchar(int c)
Print a character to debug output.
Definition cooja-log.c:87