1 /*
2 * Copyright (c) 2016, The OpenThread Authors.
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 are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "platform-simulation.h"
30 #include <openthread-core-config.h>
31 #include <openthread/config.h>
32
33 #include <ctype.h>
34 #include <errno.h>
35 #include <inttypes.h>
36 #include <stdarg.h>
37 #include <stdint.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <syslog.h>
41
42 #include <openthread/platform/logging.h>
43 #include <openthread/platform/toolchain.h>
44
45 #include "utils/code_utils.h"
46
47 #if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED)
48
49 static FILE *sLogFile = NULL;
50
platformLoggingSetFileName(const char * aName)51 void platformLoggingSetFileName(const char *aName)
52 {
53 if (sLogFile != NULL)
54 {
55 fclose(sLogFile);
56 }
57
58 sLogFile = fopen(aName, "wt");
59
60 if (sLogFile == NULL)
61 {
62 fprintf(stderr, "Failed to open log file '%s': %s\r\n", aName, strerror(errno));
63 exit(EXIT_FAILURE);
64 }
65 }
66
platformLoggingInit(const char * aName)67 void platformLoggingInit(const char *aName)
68 {
69 if (sLogFile == NULL)
70 {
71 openlog(aName, LOG_PID, LOG_USER);
72 setlogmask(setlogmask(0) & LOG_UPTO(LOG_NOTICE));
73 }
74 else
75 {
76 fprintf(sLogFile, "OpenThread logs\r\n");
77 fprintf(sLogFile, "- Program: %s\r\n", aName);
78 fprintf(sLogFile, "- Platform: simulation\r\n");
79 fprintf(sLogFile, "- Node ID: %lu\r\n", (unsigned long)gNodeId);
80 fprintf(sLogFile, "\r\n");
81 }
82 }
83
platformLoggingDeinit(void)84 void platformLoggingDeinit(void)
85 {
86 if (sLogFile != NULL)
87 {
88 fclose(sLogFile);
89 sLogFile = NULL;
90 }
91 }
92
otPlatLog(otLogLevel aLogLevel,otLogRegion aLogRegion,const char * aFormat,...)93 void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
94 {
95 OT_UNUSED_VARIABLE(aLogLevel);
96 OT_UNUSED_VARIABLE(aLogRegion);
97
98 va_list args;
99
100 va_start(args, aFormat);
101
102 if (sLogFile == NULL)
103 {
104 char logString[512];
105 int offset;
106
107 offset = snprintf(logString, sizeof(logString), "[%lu]", (unsigned long)gNodeId);
108
109 vsnprintf(&logString[offset], sizeof(logString) - (uint16_t)offset, aFormat, args);
110 syslog(LOG_CRIT, "%s", logString);
111 }
112 else
113 {
114 vfprintf(sLogFile, aFormat, args);
115 fprintf(sLogFile, "\r\n");
116 }
117
118 va_end(args);
119 }
120
121 #else
122
platformLoggingInit(const char * aName)123 void platformLoggingInit(const char *aName) { OT_UNUSED_VARIABLE(aName); }
platformLoggingDeinit(void)124 void platformLoggingDeinit(void) {}
125
126 #endif // (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED)
127