w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
RlogFile.cpp
Go to the documentation of this file.
1// $Id: RlogFile.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2011-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2018-12-19 1090 1.2.4 use RosPrintf(bool)
8// 2018-12-18 1089 1.2.3 use c++ style casts
9// 2018-12-17 1085 1.2.2 use std::lock_guard instead of boost
10// 2017-03-04 858 2.2.1 use clock_gettime instead of gettimeofday
11// 2015-01-08 631 2.2 Open(): now with RerrMsg and cout/cerr support
12// 2014-12-10 611 2.1.2 timestamp now usec precision (was msec)
13// 2013-10-11 539 2.1.1 fix date print (month was off by one)
14// 2013-02-23 492 2.1 add Name(), keep log file name; add Dump()
15// 2013-02-22 491 2.0 add Write(),IsNew(), RlogMsg iface; use lockable
16// 2011-01-30 357 1.0 Initial version
17// ---------------------------------------------------------------------------
18
23#include <time.h>
24#include <errno.h>
25
26#include <iostream>
27
28#include "RosFill.hpp"
29#include "RosPrintf.hpp"
30#include "RlogMsg.hpp"
31
32#include "RlogFile.hpp"
33
34using namespace std;
35
41// all method definitions in namespace Retro
42namespace Retro {
43
44//------------------------------------------+-----------------------------------
46
48 : fpExtStream(nullptr),
49 fIntStream(),
50 fNew(true),
51 fName(),
52 fMutex()
53{
54 ClearTime();
55}
56
57//------------------------------------------+-----------------------------------
59
60RlogFile::RlogFile(std::ostream* os, const std::string& name)
61 : fpExtStream(os),
62 fIntStream(),
63 fNew(false),
64 fName(BuildinStreamName(os, name)),
65 fMutex()
66{
67 ClearTime();
68}
69
70//------------------------------------------+-----------------------------------
72
74{}
75
76//------------------------------------------+-----------------------------------
78
79bool RlogFile::Open(std::string name, RerrMsg& emsg)
80{
81 std::ostream* os = nullptr;
82 if (name == "<cout>" || name == "-") os = &cout;
83 else if (name == "<cerr>") os = &cerr;
84 else if (name == "<clog>") os = &clog;
85 if (os) {
86 UseStream(os);
87 return true;
88 }
89
90 fNew = false;
91 fpExtStream = nullptr;
92 fName = name;
93 fIntStream.open(name.c_str());
94 if (!fIntStream.is_open())
95 emsg.InitErrno("RlogFile::Open",
96 string("open for '") + name + "' failed: ",
97 errno);
98 return fIntStream.is_open();
99}
100
101//------------------------------------------+-----------------------------------
103
105{
106 fIntStream.close();
107 return;
108}
109
110//------------------------------------------+-----------------------------------
112
113void RlogFile::UseStream(std::ostream* os, const std::string& name)
114{
115 fNew = false;
116 if (fIntStream.is_open()) Close();
117 fpExtStream = os;
118 fName = BuildinStreamName(os, name);
119 return;
120}
121
122//------------------------------------------+-----------------------------------
124
125void RlogFile::Write(const std::string& str, char tag)
126{
127 ostream& os = fpExtStream ? *fpExtStream : fIntStream;
128
129 lock_guard<RlogFile> lock(*this);
130
131 if (tag) {
132 struct timespec ts;
133 ::clock_gettime(CLOCK_REALTIME, &ts);
134
135 struct tm tymd;
136 ::localtime_r(&ts.tv_sec, &tymd);
137
138 if (tymd.tm_year != fTagYear ||
139 tymd.tm_mon != fTagMonth ||
140 tymd.tm_mday != fTagDay) {
141
142 os << "-+- "
143 << RosPrintf(tymd.tm_year+1900,"d",4) << "-"
144 << RosPrintf(tymd.tm_mon+1,"d0",2) << "-"
145 << RosPrintf(tymd.tm_mday,"d0",2) << " -+- \n";
146
147 fTagYear = tymd.tm_year;
148 fTagMonth = tymd.tm_mon;
149 fTagDay = tymd.tm_mday;
150 }
151
152 os << "-" << tag << "- "
153 << RosPrintf(tymd.tm_hour,"d0",2) << ":"
154 << RosPrintf(tymd.tm_min,"d0",2) << ":"
155 << RosPrintf(tymd.tm_sec,"d0",2) << "."
156 << RosPrintf(int(ts.tv_nsec)/1000,"d0",6) << " : ";
157 }
158
159 os << str;
160 if (str[str.length()-1] != '\n') os << endl;
161
162 return;
163}
164
165//------------------------------------------+-----------------------------------
167
168void RlogFile::Dump(std::ostream& os, int ind, const char* text) const
169{
170 RosFill bl(ind);
171 os << bl << (text?text:"--") << "RlogFile @ " << this << endl;
172 os << bl << " fpExtStream: " << fpExtStream << endl;
173 os << bl << " fIntStream.isopen " << fIntStream.is_open() << endl;
174 os << bl << " fNew " << RosPrintf(fNew) << endl;
175 os << bl << " fName " << fName << endl;
176 os << bl << " fTagYr,Mo,Dy " << fTagYear << ", " << fTagMonth
177 << ", " << fTagDay << endl;
178 return;
179}
180
181//------------------------------------------+-----------------------------------
183
185{
186 fMutex.lock();
187 return;
188}
189
190//------------------------------------------+-----------------------------------
192
194{
195 fMutex.unlock();
196 return;
197}
198
199//------------------------------------------+-----------------------------------
201
203{
204 string str = lmsg.String();
205 if (str.length() > 0) Write(str, lmsg.Tag());
206 return *this;
207}
208
209//------------------------------------------+-----------------------------------
211
213{
214 fTagYear = -1;
215 fTagMonth = -1;
216 fTagDay = -1;
217 return;
218}
219
220//------------------------------------------+-----------------------------------
222
223 std::string RlogFile::BuildinStreamName(std::ostream* os,
224 const std::string& str)
225{
226 if (str.size()) return str;
227 if (os == &cout) return string("<cout>");
228 if (os == &cerr) return string("<cerr>");
229 if (os == &clog) return string("<clog>");
230 return string("<?stream?>");
231}
232
233} // end namespace Retro
FIXME_docs.
Definition: RerrMsg.hpp:25
void InitErrno(const std::string &meth, const std::string &text, int errnum)
FIXME_docs.
Definition: RerrMsg.cpp:84
FIXME_docs.
Definition: RlogFile.hpp:34
std::ostream * fpExtStream
pointer to external stream
Definition: RlogFile.hpp:65
void Close()
FIXME_docs.
Definition: RlogFile.cpp:104
void unlock()
FIXME_docs.
Definition: RlogFile.cpp:193
int fTagYear
year of last time tag
Definition: RlogFile.hpp:69
RlogFile & operator<<(const RlogMsg &lmsg)
FIXME_docs.
Definition: RlogFile.cpp:202
int fTagMonth
month of last time tag
Definition: RlogFile.hpp:70
std::ofstream fIntStream
internal stream
Definition: RlogFile.hpp:66
void UseStream(std::ostream *os, const std::string &name="")
FIXME_docs.
Definition: RlogFile.cpp:113
~RlogFile()
Destructor.
Definition: RlogFile.cpp:73
int fTagDay
day of last time tag
Definition: RlogFile.hpp:71
std::string fName
log file name
Definition: RlogFile.hpp:68
void lock()
FIXME_docs.
Definition: RlogFile.cpp:184
bool Open(std::string name, RerrMsg &emsg)
FIXME_docs.
Definition: RlogFile.cpp:79
void ClearTime()
FIXME_docs.
Definition: RlogFile.cpp:212
bool fNew
true if never opened or used
Definition: RlogFile.hpp:67
std::mutex fMutex
mutex to lock file
Definition: RlogFile.hpp:72
RlogFile()
Default constructor.
Definition: RlogFile.cpp:47
void Dump(std::ostream &os, int ind=0, const char *text=0) const
FIXME_docs.
Definition: RlogFile.cpp:168
std::string BuildinStreamName(std::ostream *os, const std::string &str)
FIXME_docs.
Definition: RlogFile.cpp:223
void Write(const std::string &str, char tag=0)
FIXME_docs.
Definition: RlogFile.cpp:125
FIXME_docs.
Definition: RlogMsg.hpp:24
char Tag() const
FIXME_docs.
Definition: RlogMsg.ipp:38
std::string String() const
FIXME_docs.
Definition: RlogMsg.ipp:46
I/O appicator to generate fill characters.
Definition: RosFill.hpp:24
RosPrintfS< bool > RosPrintf(bool value, const char *form=0, int width=0, int prec=0)
Creates a print object for the formatted output of a bool value.
Definition: RosPrintf.ipp:38
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47