00001 /* 00002 * Copyright (c) 2006, Swedish Institute of Computer Science. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the Institute nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 * 00029 * $Id: log.c,v 1.8 2010/01/25 12:34:05 fros4943 Exp $ 00030 */ 00031 00032 #include <stdio.h> 00033 #include <stdarg.h> 00034 #include <string.h> 00035 #include "sys/log.h" 00036 #include "lib/simEnvChange.h" 00037 00038 #define IMPLEMENT_PRINTF 1 00039 00040 #if WITH_UIP 00041 /* uIP packets via SLIP */ 00042 #include "uip.h" 00043 #define MAX_LOG_LENGTH (2*UIP_BUFSIZE) 00044 #else /* WITH_UIP */ 00045 #define MAX_LOG_LENGTH 1024 00046 #endif /* WITH_UIP */ 00047 00048 #if MAX_LOG_LENGTH < 1024 00049 #undef MAX_LOG_LENGTH 00050 #define MAX_LOG_LENGTH 1024 00051 #endif /* MAX_LOG_LENGTH < 1024 */ 00052 00053 00054 const struct simInterface simlog_interface; 00055 00056 /* Variables shared between COOJA and Contiki */ 00057 char simLoggedData[MAX_LOG_LENGTH]; 00058 int simLoggedLength; 00059 char simLoggedFlag; 00060 00061 /*-----------------------------------------------------------------------------------*/ 00062 void 00063 simlog_char(char c) 00064 { 00065 if (simLoggedLength + 1 > MAX_LOG_LENGTH) { 00066 /* Dropping message due to buffer overflow */ 00067 return; 00068 } 00069 00070 simLoggedData[simLoggedLength] = c; 00071 simLoggedLength += 1; 00072 simLoggedFlag = 1; 00073 } 00074 /*-----------------------------------------------------------------------------------*/ 00075 void 00076 simlog(const char *message) 00077 { 00078 if (simLoggedLength + strlen(message) > MAX_LOG_LENGTH) { 00079 /* Dropping message due to buffer overflow */ 00080 return; 00081 } 00082 00083 memcpy(simLoggedData + simLoggedLength, message, strlen(message)); 00084 simLoggedLength += strlen(message); 00085 simLoggedFlag = 1; 00086 } 00087 /*-----------------------------------------------------------------------------------*/ 00088 void 00089 log_message(const char *part1, const char *part2) 00090 { 00091 simlog(part1); 00092 simlog(part2); 00093 } 00094 /*-----------------------------------------------------------------------------------*/ 00095 static void 00096 doInterfaceActionsBeforeTick(void) 00097 { 00098 } 00099 /*-----------------------------------------------------------------------------------*/ 00100 static void 00101 doInterfaceActionsAfterTick(void) 00102 { 00103 } 00104 /*-----------------------------------------------------------------------------------*/ 00105 #if IMPLEMENT_PRINTF 00106 int 00107 putchar(int c) 00108 { 00109 simlog_char(c); 00110 return c; 00111 } 00112 /*-----------------------------------------------------------------------------------*/ 00113 int 00114 puts(const char* s) 00115 { 00116 simlog(s); 00117 simlog_char('\n'); 00118 return 0; 00119 } 00120 /*-----------------------------------------------------------------------------------*/ 00121 int 00122 printf(const char *fmt, ...) 00123 { 00124 int res; 00125 static char buf[MAX_LOG_LENGTH]; 00126 va_list ap; 00127 va_start(ap, fmt); 00128 res = vsnprintf(buf, MAX_LOG_LENGTH, fmt, ap); 00129 va_end(ap); 00130 00131 simlog(buf); 00132 return res; 00133 } 00134 #endif /* IMPLEMENT_PRINTF */ 00135 /*-----------------------------------------------------------------------------------*/ 00136 00137 SIM_INTERFACE(simlog_interface, 00138 doInterfaceActionsBeforeTick, 00139 doInterfaceActionsAfterTick);