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