libnfc 1.8.0
log.c
1/*-
2 * Free/Libre Near Field Communication (NFC) library
3 *
4 * Libnfc historical contributors:
5 * Copyright (C) 2009 Roel Verdult
6 * Copyright (C) 2009-2013 Romuald Conty
7 * Copyright (C) 2010-2012 Romain Tartière
8 * Copyright (C) 2010-2013 Philippe Teuwen
9 * Copyright (C) 2012-2013 Ludovic Rousseau
10 * See AUTHORS file for a more comprehensive list of contributors.
11 * Additional contributors of this file:
12 *
13 * This program is free software: you can redistribute it and/or modify it
14 * under the terms of the GNU Lesser General Public License as published by the
15 * Free Software Foundation, either version 3 of the License, or (at your
16 * option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 * more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>
25 */
26
27#include "log.h"
28#include <string.h>
29#include <stdlib.h>
30#include <stdint.h>
31#include <inttypes.h>
32#include <stdio.h>
33#include <stdarg.h>
34#include <fcntl.h>
35
36const char *
37log_priority_to_str(const int priority)
38{
39 switch (priority) {
40 case NFC_LOG_PRIORITY_ERROR:
41 return "error";
42 case NFC_LOG_PRIORITY_INFO:
43 return "info";
44 case NFC_LOG_PRIORITY_DEBUG:
45 return "debug";
46 default:
47 break;
48 }
49 return "unknown";
50}
51
52
53#ifdef LOG
54
55#include "log-internal.h"
56
57void
58log_init(const nfc_context *context)
59{
60#ifdef ENVVARS
61 char str[32];
62 sprintf(str, "%"PRIu32, context->log_level);
63 setenv("LIBNFC_LOG_LEVEL", str, 1);
64#else
65 (void)context;
66#endif
67}
68
69void
70log_exit(void)
71{
72}
73
74void
75log_put(const uint8_t group, const char *category, const uint8_t priority, const char *format, ...)
76{
77 char *env_log_level = NULL;
78#ifdef ENVVARS
79 env_log_level = getenv("LIBNFC_LOG_LEVEL");
80#endif
81 uint32_t log_level;
82 if (NULL == env_log_level) {
83 // LIBNFC_LOG_LEVEL is not set
84#ifdef DEBUG
85 log_level = 3;
86#else
87 log_level = 1;
88#endif
89 } else {
90 log_level = atoi(env_log_level);
91 }
92
93 // printf("log_level = %"PRIu32" group = %"PRIu8" priority = %"PRIu8"\n", log_level, group, priority);
94 if (log_level) { // If log is not disabled by log_level=none
95 if (((log_level & 0x00000003) >= priority) || // Global log level
96 (((log_level >> (group * 2)) & 0x00000003) >= priority)) { // Group log level
97
98 va_list va;
99 va_start(va, format);
100 log_put_internal("%s\t%s\t", log_priority_to_str(priority), category);
101 log_vput_internal(format, va);
102 log_put_internal("\n");
103 va_end(va);
104 }
105 }
106}
107
108#endif // LOG
NFC library context Struct which contains internal options, references, pointers, etc....