Almetare 1.20
Alle meine Taschenrechner - Eine C++-Bibliothek zur Entwicklung von Taschenrechnern
logging.h
gehe zur Dokumentation dieser Datei
1//******************************************************************************
2// Copyright (c) 2002-2004 by Friedemann Seebass, Germany.
3// Dieses Programm ist freie Software. Siehe GNU GPL Notiz in Datei main.cpp.
4// This program is free software. See the GNU GPL notice in file main.cpp.
5// Projekt: Almetare - Alle meine Taschenrechner
6//******************************************************************************
9//******************************************************************************
10// Wann Wer Was
11// ---------- -------- ---------------------------------------------------------
12// 20.03.2003 fse dbgLog->gDbgLog, errLog->gErrLog, onlLog->gOnlLog
13// 28.03.2002 fse Alle speziellen Ausgabeoperatoren entfernt und durch
14// Template ersetzt.
15// 29.03.2002 fse Ausgabeoperatoren als Template
16// 29.11.2001 fse channelsToStr(), strToChannels() zugefuegt
17// 10.11.2001 fse writeHeader() zugefuegt
18// 19.12.2000 fse erzeugt
19//******************************************************************************
20
21#ifndef LOGGING_H
22#define LOGGING_H
23
24#ifndef __cplusplus
25#error logging.h is only for C++!
26#endif
27
28//******************************************************************************
29
30#include <iostream>
31#include <string>
32#include <fstream>
33
34using namespace std;
35
36//******************************************************************************
37
38#define DBG_LOG(channel) if (gDbgLog.configured(channel, __FILE__, __LINE__)) gDbgLog
39#define ERR_LOG(channel) if (gErrLog.configured(channel, __FILE__, __LINE__)) gErrLog
40#define ONL_LOG(channel) if (gOnlLog.configured(channel, __FILE__, __LINE__)) gOnlLog
41#define NL "\n"
42
43typedef void (*funcp)(void);
44
45//******************************************************************************
46// Log-Klasse
47//******************************************************************************
48
49enum
50{
51 ctl = 1,
52 mem = 2,
53 gnl = 4,
54 hgh = 8,
55 med = 16,
56 low = 32,
57 all = ctl + mem + gnl + hgh + med + low,
58 cns = 64
59};
60
61//******************************************************************************
62
105// fse, 19.12.02
106
107class Log
108{
109 string mFname;
110 ofstream* mFstreamP;
112 string mFileToLog;
115 string mProgName;
116 string mVersion;
117 void writeHeader();
118 int backupLogFile();
119public:
120 Log(const string& fname = "",
121 int channels = hgh | cns | med,
122 int maxLen = 50000,
123 const string& toLog = "",
124 const string& progName = "Unknown",
125 const string& version = "Vx.x");
126 ~Log();
127 int init(const string& fname,
128 int channels,
129 int maxLogLen,
130 const string& fileToLog,
131 const string& progName,
132 const string& version);
133 ofstream* getFstreamP() { return mFstreamP; }
134 int getConfChannels() const { return mConfChannels; }
135 void stopLogging();
136 void setConfChannels(int chans) { mConfChannels = chans; }
137 static string getDate();
138 static string getTime();
139 static string channelsToStr(int channels);
140 static int strToChannels(const string& str);
141 bool configured(int channels, const char* file, int line);
142 ios::fmtflags setf(ios::fmtflags format) { return mFstreamP->setf(format); }
143 // Ausgabeoperatoren:
144// Log& operator<<(const char* out);
145// Log& operator<<(const string& out);
146// Log& operator<<(string& out);
147// Log& operator<<(const int out);
148// Log& operator<<(const float out);
149// Log& operator<<(const double out);
150// Log& operator<<(const void* out);
151// Log& operator<<(const unsigned int out);
152// Log& operator<<(Symbol& out);
153// Log& operator<<(Computer& out);
154// Log& operator<<(CalcDisplay& out);
155// Log& operator<<(const funcp);
156 // geht leider nicht mit VC++, nur mit GNU (s. Kommentar bei Node::new()):
157//#ifdef _MSC_VER
158// Log& operator<<(_Smanip<int> out);
159//#else
160// Log& operator<<(smanip<int> out);
161//#endif
162};
163
164//******************************************************************************
165
169// fse, 28.03.02
170
171template<class T> Log& operator<<(Log& log, const T& out)
172{
173 if (log.getFstreamP() ) *log.getFstreamP() << out;
174 if (log.getConfChannels() & cns) cout << out;
175 return log;
176}
177
178//******************************************************************************
179
180extern Log gDbgLog;
181extern Log gErrLog;
182extern Log gOnlLog;
183void logFlush(void);
184void errFlush(void);
185void onlFlush(void);
186
187#endif // !LOGGING_H
Klasse zum Loggen von Laufzeitausgaben in eine Datei.
Definition: logging.h:108
void stopLogging()
Schliesst Datei und loescht den allokierten ofstream.
Definition: logging.cpp:155
string mFileToLog
Name der Quelltextdatei, die als einzige geloggt wird.
Definition: logging.h:112
void writeHeader()
Schreibt Datei-Header.
Definition: logging.cpp:180
Log(const string &fname="", int channels=hgh|cns|med, int maxLen=50000, const string &toLog="", const string &progName="Unknown", const string &version="Vx.x")
Vorlaeufige Konstruktion eines Log-Objektes.
Definition: logging.cpp:51
int init(const string &fname, int channels, int maxLogLen, const string &fileToLog, const string &progName, const string &version)
Initialisiert die Dateinamen und Channels.
Definition: logging.cpp:90
int mMaxLogLen
maximale Laenge der Log-Dateien in Byte
Definition: logging.h:113
static string getTime()
Gibt die Systemuhrzeit als String im Format "hh:mm:ss" zurueck.
Definition: logging.cpp:305
string mVersion
Version des Programms (fuer Titelzeile der Log-Datei)
Definition: logging.h:116
static string getDate()
Gibt String mit dem aktuellen Datum der Systemuhr im Format "Wed Dec 21 2000" zurueck.
Definition: logging.cpp:196
string mProgName
Name des Programms (fuer Titelzeile der Log-Datei)
Definition: logging.h:115
string mFname
Name d. Datei, in die geloggt wird ("" = Konsole)
Definition: logging.h:109
static int strToChannels(const string &str)
Wandelt String str in channels um.
Definition: logging.cpp:358
int backupLogFile()
Benennt die Logdatei *.txt in *.bak um und loescht zuvor eine aeltere *.bak-Datei,...
Definition: logging.cpp:261
bool mIsSuppressed
Ausgabe unterdrueckt.
Definition: logging.h:114
static string channelsToStr(int channels)
Wandelt channels ch in einen String um.
Definition: logging.cpp:335
ofstream * mFstreamP
Filestream der Datei.
Definition: logging.h:110
int mConfChannels
Kanaele, die konfiguriert sind (z. B. per Ini-Datei)
Definition: logging.h:111
bool configured(int channels, const char *file, int line)
Prueft, ob die geforderten Kanaele ueberhaupt "eingeschaltet" sind und die Groesse des Logfiles.
Definition: logging.cpp:228
Log & operator<<(Log &log, const T &out)
Globales Funktions-Template.
Definition: logging.h:171
@ hgh
"High" - wichtige Ausgaben
Definition: logging.h:54
@ low
"Low" - unwichtige Ausgaben
Definition: logging.h:56
@ gnl
"General" - allgemeine Ausgaben
Definition: logging.h:53
@ med
"Medium" - normale Ausgaben
Definition: logging.h:55
@ ctl
"Control" - Kontrollfluss
Definition: logging.h:51
@ mem
"Memory" - Speicherverwaltung
Definition: logging.h:52
@ cns
"Console" - Ausgaben zusaetzlich auf Konsole
Definition: logging.h:58