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.cpp
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 #include "log.h"
21 #include <iostream>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdarg.h>
25 #include <ctime>
26 #include <time.h>
27 #include <fstream>
28 #include "../adapters/filemanager.h"
29 
30 
31 const std::string Log::LOG_NAME("entropy_tuner_log.txt");
32 std::shared_ptr<Log> Log::mLog;
33 
34 Log::Log(bool useLogfile) {
35  mLog = std::shared_ptr<Log>(this);
36  if (useLogfile) {
38  mLogStream,
39  FileManager::getSingleton().getLogFilePath(LOG_NAME));
40  writeToLogfile(LEVEL_INFORMATION, "Log file for entropy tuner\n\n", __LINE__, __FILE__, __func__);
41  }
42 }
43 
45 {
46  mLogStream.close();
47 }
48 
49 const char* Log::simplify (const char* filename)
50 {
51  const char* pattern = "Entropy-Piano-Tuner/";
52  int patternsize = strlen(pattern);
53  int len = strlen(filename);
54  if (len > 0)
55  {
56  const char *substring = strstr(filename, pattern);
57  if (substring != NULL) filename = substring + patternsize;
58  }
59  return filename;
60 }
61 
62 void Log::verbose(const char *text, int line, const char *file, const char *function) {
63  char buffer[ERROR_BUFFER_SIZE];
64  snprintf(buffer, ERROR_BUFFER_SIZE, "%s (line %d in file %s in function %s)", text, line, simplify(file), function);
65  mLog->impl_verbose(buffer);
66  mLog->writeToLogfile(LEVEL_VERBOSE, text, line, file, function);
67 }
68 
69 void Log::debug(const char *text, int line, const char *file, const char *function) {
70  char buffer[ERROR_BUFFER_SIZE];
71  snprintf(buffer, ERROR_BUFFER_SIZE, "%s (line %d in file %s in function %s)", text, line, simplify(file), function);
72  mLog->impl_debug(buffer);
73  mLog->writeToLogfile(LEVEL_DEBUG, text, line, file, function);
74 }
75 
76 void Log::information(const char *text, int line, const char *file, const char *function) {
77  char buffer[ERROR_BUFFER_SIZE];
78  snprintf(buffer, ERROR_BUFFER_SIZE, "%s (line %d in file %s in function %s)", text, line, simplify(file), function);
79  mLog->impl_information(buffer);
80  mLog->writeToLogfile(LEVEL_INFORMATION, text, line, file, function);
81 }
82 
83 void Log::warning(const char *text, int line, const char *file, const char *function) {
84  char buffer[ERROR_BUFFER_SIZE];
85  snprintf(buffer, ERROR_BUFFER_SIZE, "%s (line %d in file %s in function %s)", text, line, simplify(file), function);
86  mLog->impl_warning(buffer);
87  mLog->writeToLogfile(LEVEL_WARNING, text, line, file, function);
88 }
89 
90 void Log::error(const char *text, int line, const char *file, const char *function) {
91  char buffer[ERROR_BUFFER_SIZE];
92  snprintf(buffer, ERROR_BUFFER_SIZE, "%s (line %d in file %s in function %s)", text, line, file, function);
93  mLog->impl_error(buffer);
94  mLog->writeToLogfile(LEVEL_ERROR, text, line, file, function);
95 }
96 
97 void Log::impl_verbose(const char *l) {
98  std::cout << "Verbose: " << l << std::endl;
99 }
100 
101 void Log::impl_debug(const char *l) {
102  std::cout << "Debug: " << l << std::endl;
103 }
104 
105 void Log::impl_information(const char *l) {
106  std::cout << "Information: " << l << std::endl;
107 }
108 
109 void Log::impl_warning(const char *l) {
110  std::cout << "Warning: " << l << std::endl;
111 }
112 
113 void Log::impl_error(const char *l) {
114  std::cout << "Error: " << l << std::endl;
115 }
116 
117 void Log::writeToLogfile(ELevel level, const char *text, int line, const char *file, const char *function)
118 {
119  if (mLogStream.is_open() == false) return;
120 
121  // current time
122  std::time_t t = std::time(0); //obtain the current time_t value
123  tm now;
124 #ifdef _MSC_VER
125  localtime_s(&now, &t); //convert it to tm
126 #else
127  now = *localtime(&t);
128 #endif
129  char tmdescr[20]={0};
130  const char fmt[]="%X";
131  strftime(tmdescr, sizeof(tmdescr) - 1, fmt, &now);
132 
133  // write level to log file
134  switch (level) {
135  case LEVEL_VERBOSE:
136  mLogStream << "V/";
137  break;
138  case LEVEL_DEBUG:
139  mLogStream << "D/";
140  break;
141  case LEVEL_INFORMATION:
142  mLogStream << "I/";
143  break;
144  case LEVEL_WARNING:
145  mLogStream << "W/";
146  break;
147  case LEVEL_ERROR:
148  mLogStream << "E/";
149  break;
150  }
151 
152  // write time to log file:
153  mLogStream << tmdescr << ":\t";
154 
155  mLogStream << "In file " << simplify(file) << " in function " << function << " at line " << line << std::endl;
156 
157  // write to the log file
158  mLogStream << "\t\t" << text << std::endl << std::endl;
159  mLogStream.flush();
160 }
static void error(const char *text, int line, const char *file, const char *function)
Definition: log.cpp:90
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
static FileManager & getSingleton()
FileManager::getSingleton: Get a reference to the singleton.
Definition: filemanager.cpp:47
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
#define ERROR_BUFFER_SIZE
Definition: log.h:30
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
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
virtual void impl_warning(const char *l)
Definition: log.cpp:109
virtual bool open(std::ifstream &stream, const std::string &absolute, std::ios_base::openmode mode=std::ios_base::in)
Open an input stream.
Definition: filemanager.cpp:71
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