Entropy Piano Tuner  1.1.3 (documentation not yet complete)
An open-source experimental software for piano tuning by entropy minimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
log.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2015 Haye Hinrichsen, Christoph Wick
3  *
4  * This file is part of Entropy Piano Tuner.
5  *
6  * Entropy Piano Tuner is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * Entropy Piano Tuner is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * Entropy Piano Tuner. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 #ifndef LOG_H
21 #define LOG_H
22 
23 #include <memory>
24 #include <fstream>
25 #include <cstdio>
26 #include "../config.h"
27 #include "prerequisites.h"
28 
29 
30 #define ERROR_BUFFER_SIZE 1024
31 
32 // Log level range check
33 #if LOG_LEVEL < 1 || LOG_LEVEL > 5
34 # error Log level has to be in [1, 5], see config.h
35 #endif
36 
37 #if LOG_LEVEL <= 1
38 # define LogV(...) {char b[ERROR_BUFFER_SIZE]; snprintf(b, ERROR_BUFFER_SIZE, __VA_ARGS__); Log::verbose(b, __LINE__, __FILE__, __func__);}
39 #else
40 # define LogV(...)
41 #endif
42 
43 #if LOG_LEVEL <= 2
44 # define LogD(...) {char b[ERROR_BUFFER_SIZE]; snprintf(b, ERROR_BUFFER_SIZE, __VA_ARGS__); Log::debug(b, __LINE__, __FILE__, __func__);}
45 #else
46 # define LogD(...)
47 #endif
48 
49 #if LOG_LEVEL <= 3
50 # define LogI(...) {char b[ERROR_BUFFER_SIZE]; snprintf(b, ERROR_BUFFER_SIZE, __VA_ARGS__); Log::information(b, __LINE__, __FILE__, __func__);}
51 #else
52 # define LogI(...)
53 #endif
54 
55 #if LOG_LEVEL <= 4
56 # define LogW(...) {char b[ERROR_BUFFER_SIZE]; snprintf(b, ERROR_BUFFER_SIZE, __VA_ARGS__); Log::warning(b, __LINE__, __FILE__, __func__);}
57 #else
58 # define LogW(...)
59 #endif
60 
61 #if LOG_LEVEL <= 5
62 # define LogE(...) {char b[ERROR_BUFFER_SIZE]; snprintf(b, ERROR_BUFFER_SIZE, __VA_ARGS__); Log::error(b, __LINE__, __FILE__, __func__);}
63 #else
64 # define LogE(...)
65 #endif
66 
67 class Log
68 {
69 public:
70  static const std::string LOG_NAME;
71 
72 private:
73  enum ELevel {
79  };
80 
81 public:
82  static void verbose(const char *text, int line, const char *file, const char *function);
83  static void debug(const char *text, int line, const char *file, const char *function);
84  static void information(const char *text, int line, const char *file, const char *function);
85  static void warning(const char *text, int line, const char *file, const char *function);
86  static void error(const char *text, int line, const char *file, const char *function);
87 
88  Log(bool useLogfile = true);
89  ~Log();
90 
91  static Log &getSingleton() {return *mLog.get();}
92  static Log *getSingletonPtr() {return mLog.get();}
93 
94 protected:
95  virtual void impl_verbose(const char *l);
96  virtual void impl_debug(const char *l);
97  virtual void impl_information(const char *l);
98  virtual void impl_warning(const char *l);
99  virtual void impl_error(const char *l);
100 
101 private:
102  void writeToLogfile(ELevel level, const char *text, int line, const char *file, const char *function);
103 
104 private:
105  static std::shared_ptr<Log> mLog;
106  std::ofstream mLogStream;
107  static const char* simplify (const char* filename);
108 };
109 
110 #endif // LOG_H
static void error(const char *text, int line, const char *file, const char *function)
Definition: log.cpp:90
static Log & getSingleton()
Definition: log.h:91
ELevel
Definition: log.h:73
static void verbose(const char *text, int line, const char *file, const char *function)
Definition: log.cpp:62
static void information(const char *text, int line, const char *file, const char *function)
Definition: log.cpp:76
virtual void impl_error(const char *l)
Definition: log.cpp:113
virtual void impl_debug(const char *l)
Definition: log.cpp:101
std::ofstream mLogStream
Definition: log.h:106
static const char * simplify(const char *filename)
Definition: log.cpp:49
Log(bool useLogfile=true)
Definition: log.cpp:34
static void warning(const char *text, int line, const char *file, const char *function)
Definition: log.cpp:83
void writeToLogfile(ELevel level, const char *text, int line, const char *file, const char *function)
Definition: log.cpp:117
static Log * getSingletonPtr()
Definition: log.h:92
virtual void impl_verbose(const char *l)
Definition: log.cpp:97
static std::shared_ptr< Log > mLog
Definition: log.h:105
~Log()
Definition: log.cpp:44
static const std::string LOG_NAME
Definition: log.h:70
Definition: log.h:67
virtual void impl_warning(const char *l)
Definition: log.cpp:109
virtual void impl_information(const char *l)
Definition: log.cpp:105
static void debug(const char *text, int line, const char *file, const char *function)
Definition: log.cpp:69