Project Alice
Loading...
Searching...
No Matches
pcp_logger.c
Go to the documentation of this file.
1/*
2 Copyright (c) 2014 by Cisco Systems, Inc.
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
8 1. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifdef HAVE_CONFIG_H
27#include "config.h"
28#else
29#include "default_config.h"
30#endif
31
32#ifdef _MSC_VER
33#define _CRT_SECURE_NO_WARNINGS 1
34#endif
35
36#include <stdarg.h>
37#include <stdio.h>
38#include <stdint.h>
39#include <stdlib.h>
40#include <string.h>
41#include "pcp.h"
42#include "pcp_logger.h"
43#ifdef _MSC_VER
44#include "windows/pcp_gettimeofday.h" //gettimeofday()
45#else
46#include "sys/time.h"
47#endif //_MSC_VER
49
51{
52 char *env, *ret;
53 env = getenv("PCP_LOG_LEVEL");
54 if(env) {
55 long lvl=strtol(env, &ret, 0);
56 if ((ret) && (!*ret) && (lvl>=0) && (lvl<=PCP_MAX_LOG_LEVEL)) {
58 }
59 }
60}
61
62static void default_logfn(pcp_loglvl_e mode, const char *msg)
63{
64
65 const char *prefix;
66 static struct timeval prev_timestamp={0, 0};
67 struct timeval cur_timestamp;
68 uint64_t diff;
69
70 gettimeofday(&cur_timestamp, NULL);
71
72 if ((prev_timestamp.tv_sec == 0) && (prev_timestamp.tv_usec == 0)) {
73 prev_timestamp=cur_timestamp;
74 diff=0;
75 } else {
76 diff=(cur_timestamp.tv_sec - prev_timestamp.tv_sec) * 1000000
77 + (cur_timestamp.tv_usec - prev_timestamp.tv_usec);
78 }
79
80 switch (mode) {
81 case PCP_LOGLVL_ERR:
82 case PCP_LOGLVL_PERR:
83 prefix="ERROR";
84 break;
85 case PCP_LOGLVL_WARN:
86 prefix="WARNING";
87 break;
88 case PCP_LOGLVL_INFO:
89 prefix="INFO";
90 break;
92 prefix="DEBUG";
93 break;
94 default:
95 prefix="UNKNOWN";
96 break;
97 }
98
99 fprintf(stderr, "%3llus %03llums %03lluus %-7s: %s\n",
100 (long long int)diff / 1000000,
101 (long long int)(diff % 1000000) / 1000, (long long int)diff % 1000,
102 prefix, msg);
103}
104
105static external_logger logger=default_logfn;
106
108{
109 logger=ext_log;
110}
111
112void pcp_logger(pcp_loglvl_e log_level, const char *fmt, ...)
113{
114 int n;
115 int size=256; /* Guess we need no more than 256 bytes. */
116 char *p, *np;
117 va_list ap;
118
119 if (log_level > pcp_log_level) {
120 return;
121 }
122
123 p = (char*)malloc(size);
124 if (!p) {
125 return; //LCOV_EXCL_LINE
126 }
127
128 while (1) {
129
130 /* Try to print in the allocated space. */
131
132 va_start(ap, fmt);
133 n=vsnprintf(p, size, fmt, ap);
134 va_end(ap);
135
136 /* If that worked, return the string. */
137
138 if (n > -1 && n < size) {
139 break;
140 }
141
142 /* Else try again with more space. */
143
144 if (n > -1) /* glibc 2.1 */
145 size=n + 1; /* precisely what is needed */
146 else
147 /* glibc 2.0 */
148 size*=2; /* twice the old size */ //LCOV_EXCL_LINE
149
150 np = (char*)realloc(p, size);
151 if (!np) {
152 free(p); //LCOV_EXCL_LINE
153 return; //LCOV_EXCL_LINE
154 }
155 p=np;
156 }
157
158 if (logger)
159 (*logger)(log_level, p);
160
161 free(p);
162 return;
163}
164
165void pcp_strerror(int errnum, char *buf, size_t buflen)
166{
167
168 memset(buf, 0, buflen);
169
170#ifdef WIN32
171
172 strerror_s(buf, buflen, errnum);
173
174#else //WIN32
175 strerror_r(errnum, buf, buflen);
176#endif //WIN32
177}
178
ulong uint64_t
void(* external_logger)(pcp_loglvl_e, const char *)
Definition: pcp.h:93
pcp_loglvl_e
Definition: pcp.h:84
@ PCP_LOGLVL_ERR
Definition: pcp.h:86
@ PCP_LOGLVL_DEBUG
Definition: pcp.h:90
@ PCP_LOGLVL_INFO
Definition: pcp.h:88
@ PCP_LOGLVL_PERR
Definition: pcp.h:89
@ PCP_LOGLVL_WARN
Definition: pcp.h:87
int gettimeofday(struct timeval *tv, struct timezone *tz)
pcp_loglvl_e pcp_log_level
Definition: pcp_logger.c:48
void pcp_set_loggerfn(external_logger ext_log)
Definition: pcp_logger.c:107
void pcp_strerror(int errnum, char *buf, size_t buflen)
Definition: pcp_logger.c:165
void pcp_logger(pcp_loglvl_e log_level, const char *fmt,...)
Definition: pcp_logger.c:112
void pcp_logger_init(void)
Definition: pcp_logger.c:50
#define PCP_MAX_LOG_LEVEL
Definition: pcp_logger.h:68