Contiki 2.5
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  * $Id: log.c,v 1.8 2010/01/25 12:34:05 fros4943 Exp $
30  */
31 
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include "sys/log.h"
36 #include "lib/simEnvChange.h"
37 
38 #define IMPLEMENT_PRINTF 1
39 
40 #if WITH_UIP
41 /* uIP packets via SLIP */
42 #include "uip.h"
43 #define MAX_LOG_LENGTH (2*UIP_BUFSIZE)
44 #else /* WITH_UIP */
45 #define MAX_LOG_LENGTH 1024
46 #endif /* WITH_UIP */
47 
48 #if MAX_LOG_LENGTH < 1024
49 #undef MAX_LOG_LENGTH
50 #define MAX_LOG_LENGTH 1024
51 #endif /* MAX_LOG_LENGTH < 1024 */
52 
53 
54 const struct simInterface simlog_interface;
55 
56 /* Variables shared between COOJA and Contiki */
57 char simLoggedData[MAX_LOG_LENGTH];
58 int simLoggedLength;
59 char simLoggedFlag;
60 
61 /*-----------------------------------------------------------------------------------*/
62 void
63 simlog_char(char c)
64 {
65  if (simLoggedLength + 1 > MAX_LOG_LENGTH) {
66  /* Dropping message due to buffer overflow */
67  return;
68  }
69 
70  simLoggedData[simLoggedLength] = c;
71  simLoggedLength += 1;
72  simLoggedFlag = 1;
73 }
74 /*-----------------------------------------------------------------------------------*/
75 void
76 simlog(const char *message)
77 {
78  if (simLoggedLength + strlen(message) > MAX_LOG_LENGTH) {
79  /* Dropping message due to buffer overflow */
80  return;
81  }
82 
83  memcpy(simLoggedData + simLoggedLength, message, strlen(message));
84  simLoggedLength += strlen(message);
85  simLoggedFlag = 1;
86 }
87 /*-----------------------------------------------------------------------------------*/
88 void
89 log_message(const char *part1, const char *part2)
90 {
91  simlog(part1);
92  simlog(part2);
93 }
94 /*-----------------------------------------------------------------------------------*/
95 static void
96 doInterfaceActionsBeforeTick(void)
97 {
98 }
99 /*-----------------------------------------------------------------------------------*/
100 static void
101 doInterfaceActionsAfterTick(void)
102 {
103 }
104 /*-----------------------------------------------------------------------------------*/
105 #if IMPLEMENT_PRINTF
106 int
107 putchar(int c)
108 {
109  simlog_char(c);
110  return c;
111 }
112 /*-----------------------------------------------------------------------------------*/
113 int
114 puts(const char* s)
115 {
116  simlog(s);
117  simlog_char('\n');
118  return 0;
119 }
120 /*-----------------------------------------------------------------------------------*/
121 int
122 printf(const char *fmt, ...)
123 {
124  int res;
125  static char buf[MAX_LOG_LENGTH];
126  va_list ap;
127  va_start(ap, fmt);
128  res = vsnprintf(buf, MAX_LOG_LENGTH, fmt, ap);
129  va_end(ap);
130 
131  simlog(buf);
132  return res;
133 }
134 #endif /* IMPLEMENT_PRINTF */
135 /*-----------------------------------------------------------------------------------*/
136 
137 SIM_INTERFACE(simlog_interface,
138  doInterfaceActionsBeforeTick,
139  doInterfaceActionsAfterTick);