USBUART
A library for reading/wring data via USB-UART adapters
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules
log.cpp
Go to the documentation of this file.
1 
5 /* This file is part of USBUART Library. http://usbuart.info/
6  *
7  * Copyright (C) 2016 Eugene Hutorny <eugene@hutorny.in.ua>
8  *
9  * The USBUART Library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License v2
11  * as published by the Free Software Foundation;
12  *
13  * The USBUART Library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with the USBUART Library; if not, see
20  * <http://www.gnu.org/licenses/gpl-2.0.html>.
21  */
22 
23 #include <cstdio>
24 #include <cstdarg>
25 #include <cstring>
26 #include "usbuart.hpp"
27 namespace usbuart {
28 Log log;
29 
30 static inline const char* strchrr(const char* s, const char* e, char c) {
31  while( s != e && *e != c ) --e;
32  return e;
33 }
34 
35 static void vlogf(loglevel_t lvl,
36  const char *tag, const char * fmt, va_list args) {
37  static const char* const L[] = {" ","error","warn ","info ", "debug"};
38  int l = static_cast<int>(lvl);
39  static constexpr int W=28;
40  if( tag ) {
41  const char* end = strchr(tag,'(');
42  const char* beg = end ? strchrr(tag, end,' ') : strrchr(tag, ' ');
43  if( ! beg ) beg = tag;
44  else if( *beg == ' ' ) ++beg;
45  if( end - beg > W ) {
46  beg = end - (W-3);
47  fprintf(stderr, "{...%*.*s} %s ", W-3, W-3, beg, L[l]);
48  }
49  else
50  fprintf(stderr, "{%*.*s} %s ", W, (end-beg), beg, L[l]);
51  } else{
52  fprintf(stderr, "{} %s ", L[l]);
53  }
54  vfprintf(stderr, fmt, args);
55  if( ! strrchr(fmt, '\n') )
56  fputs("\n", stderr);
57 }
58 
59 
60 void Log::e(const char *tag, const char *fmt, ...) noexcept {
61  if( level < loglevel_t::error ) return;
62  va_list args;
63  va_start(args, fmt);
64  vlogf(loglevel_t::error, tag, fmt, args);
65  va_end(args);
66 }
67 
68 void Log::w(const char *tag, const char *fmt, ...) noexcept {
69  if( level < loglevel_t::warning ) return;
70  va_list args;
71  va_start(args, fmt);
72  vlogf(loglevel_t::warning, tag, fmt, args);
73  va_end(args);
74 }
75 
76 void Log::i(const char *tag, const char *fmt, ...) noexcept {
77  if( level < loglevel_t::info ) return;
78  va_list args;
79  va_start(args, fmt);
80  vlogf(loglevel_t::info,tag, fmt, args);
81  va_end(args);
82 }
83 
84 void Log::d(const char *tag, const char *fmt, ...) noexcept {
85  if( level < loglevel_t::debug ) return;
86  va_list args;
87  va_start(args, fmt);
88  vlogf(loglevel_t::debug, tag, fmt, args);
89  va_end(args);
90 }
91 } /* namespace usbuart */
implementation header USBUART Library
USBUART namespace.
Definition: usbuart.h:29