Arduino DLNA Server
Logger.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include "Arduino.h"
7 
8 #define DLNA_MAX_LOG_SIZE 800
9 
10 namespace tiny_dlna {
11 
17 
18 static const char* HttpDlnaLogLevelStr[] = {"Debug", "Info", "Warning", "Error"};
19 
25 class LoggerClass {
26  public:
27 
28  // activate the logging
29  virtual void begin(Print& out, DlnaLogLevel level = DlnaError) {
30  this->log_stream_ptr = &out;
31  this->log_level = level;
32  }
33 
34  void setLevel(DlnaLogLevel l) { log_level = l; }
35 
36  // checks if the logging is active
37  virtual bool isLogging() { return log_stream_ptr != nullptr; }
38 
40  void log(DlnaLogLevel current_level, const char* fmt...) {
41  if (current_level >= log_level && log_stream_ptr != nullptr &&
42  fmt != nullptr) {
43  char log_buffer[DLNA_MAX_LOG_SIZE];
44  strcpy(log_buffer, "DLNA - ");
45  strcat(log_buffer, HttpDlnaLogLevelStr[current_level]);
46  strcat(log_buffer, ": ");
47  va_list arg;
48  va_start(arg, fmt);
49  vsnprintf(log_buffer + 15, DLNA_MAX_LOG_SIZE - 15, fmt, arg);
50  va_end(arg);
51  log_stream_ptr->println(log_buffer);
52  }
53  }
54 
55  protected:
56  Print* log_stream_ptr = &Serial;
58 };
59 
60 extern LoggerClass DlnaLogger;
61 
62 } // namespace tiny_dlna
#define DLNA_MAX_LOG_SIZE
Definition: Logger.h:8
Logger that writes messages dependent on the log level.
Definition: Logger.h:25
DlnaLogLevel log_level
Definition: Logger.h:57
virtual void begin(Print &out, DlnaLogLevel level=DlnaError)
Definition: Logger.h:29
Print * log_stream_ptr
Definition: Logger.h:56
void log(DlnaLogLevel current_level, const char *fmt...)
Print log message.
Definition: Logger.h:40
void setLevel(DlnaLogLevel l)
Definition: Logger.h:34
virtual bool isLogging()
Definition: Logger.h:37
Definition: Allocator.h:6
DlnaLogLevel
Supported log levels.
Definition: Logger.h:16
@ DlnaDebug
Definition: Logger.h:16
@ DlnaInfo
Definition: Logger.h:16
@ DlnaWarning
Definition: Logger.h:16
@ DlnaError
Definition: Logger.h:16
LoggerClass DlnaLogger
Definition: Logger.cpp:5