General Purpose Geodetic Library
SgLogger.cpp
Go to the documentation of this file.
1 /*
2  *
3  * This file is a part of Space Geodetic Library. The library is used by
4  * nuSolve, a part of CALC/SOLVE system, and designed to make analysis of
5  * geodetic VLBI observations.
6  * Copyright (C) 2010-2020 Sergei Bolotin.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 
24 #include <QtCore/QDir>
25 #include <QtCore/QFile>
26 #include <QtCore/QTextStream>
27 
28 
29 
30 #include <SgLogger.h>
31 #include <SgMJD.h>
32 
33 
34 /*=======================================================================================================
35 *
36 * METHODS:
37 *
38 *======================================================================================================*/
39 SgLogger::SgLogger(int capacity, bool isStoreInFile, const QString& fileName) :
40  dirName_ (""),
41  fileName_(fileName),
42  logSupplements_()
43 {
44  capacity_ = capacity;
45  isStoreInFile_= isStoreInFile;
46 
47  logFacilities_[ERR] = 0xFFFFFFFF;
48  logFacilities_[WRN] = 0xFFFFFFFF;
49  logFacilities_[INF] = 0xFFFFFFFF;
51  isNeedTimeMark_ = true;
52  useFullDateFormat_ = false;
53 // useFullDateFormat_ = true;
54  isMute_ = false;
55  facilitiesSerialNumber_ = 20120815.0;
56 };
57 
58 
59 
60 //
62 {
63  if (spool_.size())
64  clearSpool();
65  if (logSupplements_.size())
66  logSupplements_.clear(); // we are not an owner
67  while (!spool_.isEmpty())
68  delete spool_.takeFirst();
69 };
70 
71 
72 
73 //
75 {
76  //just explain:
77  QString tmp;
78  tmp.sprintf("Capacity: %d", capacity_);
79  if (isStoreInFile_)
80  tmp += "; log file name: \"" + fileName_ + "\"";
81  write(DBG, IO_TXT, className() + ": started with parameters: " + tmp);
82 };
83 
84 
85 
86 // all objects should use SgLogger::write(level, facility, const QString&)
87 // to log something
88 void SgLogger::write(LogLevel level, quint32 facility, const QString &s, bool isAsync)
89 {
90  if (logSupplements_.size())
91  for (QMap<QString, SgLogger*>::iterator it=logSupplements_.begin(); it!=logSupplements_.end(); ++it)
92  it.value()->write(level, facility, s, isAsync);
93  if (isEligible(level, facility))
94  {
95  QString str("");
96  if (isNeedTimeMark_)
97 // str = SgMJD::currentMJD().toString( useFullDateFormat_?SgMJD::F_RFC2822:SgMJD::F_HHMMSS ) + " ";
98 // str = SgMJD::currentMJD().toString( useFullDateFormat_?SgMJD::F_ISO:SgMJD::F_HHMMSS ) + " ";
100  " ";
101  if (level == ERR)
102  str += "ERROR: ";
103  if (level == WRN)
104  str += "Warning: ";
105  str += s;
106  spool_.append(new QString(str));
107  makeOutput(level, str, isAsync);
108  if (spool_.count() == capacity_)
109  clearSpool();
110  };
111 };
112 
113 
114 
115 //
117 {
118  if (isStoreInFile_)
119  {
120  QFile file2write(dirName_.isEmpty()?fileName_:dirName_ + "/" + fileName_);
121  // check the path:
122  if (!dirName_.isEmpty())
123  {
124  QDir dir;
125  if (!dir.exists(dirName_))
126  if (!dir.mkpath(dirName_))
127  std::cout << "SgLogger::clearSpool(): cannot create direcory \"" << qPrintable(dirName_)
128  << "\"\n";
129  };
130  if (file2write.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Unbuffered))
131  {
132  QTextStream ts(&file2write);
133  for (int i=0; i<spool_.size(); i++)
134  ts << *spool_.at(i) << endl;
135  ts.setDevice(NULL);
136  file2write.close();
137  }
138  else
139  std::cout << "SgLogger::clearSpool(): cannot open log file \"" << file2write.error() << "\"\n";
140  };
141  while (!spool_.isEmpty())
142  delete spool_.takeFirst();
143 };
144 
145 
146 
147 //
149 {
150 // if (isStoreInFile_) //
151 // {
152  QString target(dirName_.isEmpty() ? fileName_ : dirName_ + "/" + fileName_);
153  if (QFile::exists(target))
154  if (!QFile::remove(target))
156  "::rmLogFile(): cannot remove the file \"" + target + "\"");
157 // };
158 };
159 
160 
161 
162 //
163 void SgLogger::makeOutput(LogLevel, const QString &s, bool)
164 {
165  if (!isMute_)
166  std::cerr << qPrintable(s) << "\n";
167 };
168 
169 
170 
171 //
172 void SgLogger::attachSupplementLog(const QString& name, SgLogger *auxLogger)
173 {
174  if (!auxLogger)
176  "::attachSupplementLog(): the log \"" + name + "\" is NULL");
177  else if (logSupplements_.contains(name))
179  "::attachSupplementLog(): the log \"" + name + "\" was already added to the map");
180  else
181  logSupplements_.insert(name, auxLogger);
182 };
183 
184 
185 
186 //
187 void SgLogger::detachSupplementLog(const QString& name)
188 {
189  if (logSupplements_.contains(name))
190  logSupplements_.remove(name);
191  else
193  "::detachSupplementLog(): the log \"" + name + "\" is not in the map");
194 };
195 
196 
197 
198 //
200 {
201  return logSupplements_.contains(name)?logSupplements_[name]:NULL;
202 };
203 /*=======================================================================================================
204 *
205 * FRIENDS:
206 *
207 *======================================================================================================*/
208 //
209 
210 
211 
212 
213 
214 
215 /*=====================================================================================================*/
216 //
217 // aux functions:
218 //
219 
220 
221 
222 // i/o:
223 
224 
225 /*=====================================================================================================*/
226 //
227 // constants:
228 //
230 //SgLogger *logger=new SgLogger();
232 
233 
234 /*=====================================================================================================*/
SgLogger * logger
Definition: SgLogger.cpp:231
SgLogger basicLogger
Definition: SgLogger.cpp:202
bool isMute_
Definition: SgLogger.h:178
virtual void write(LogLevel, quint32, const QString &, bool=false)
Definition: SgLogger.cpp:88
QString fileName_
Definition: SgLogger.h:180
SgLogger(int capacity=100, bool isStoreInFile=false, const QString &fileName="logger.log")
Definition: SgLogger.cpp:39
void attachSupplementLog(const QString &name, SgLogger *auxLogger)
Definition: SgLogger.cpp:172
SgLogger * lookupSupplementLog(const QString &name)
Definition: SgLogger.cpp:199
QString dirName_
Definition: SgLogger.h:179
virtual void makeOutput(LogLevel level, const QString &s, bool isAsync)
Definition: SgLogger.cpp:163
QMap< QString, SgLogger * > logSupplements_
Definition: SgLogger.h:185
@ ESTIMATOR
Definition: SgLogger.h:90
@ IO_TXT
Definition: SgLogger.h:65
@ DATA
Definition: SgLogger.h:78
@ PREPROC
Definition: SgLogger.h:98
@ CONFIG
Definition: SgLogger.h:93
QString className() const
Definition: SgLogger.h:148
virtual void clearSpool()
Definition: SgLogger.cpp:116
virtual ~SgLogger()
Definition: SgLogger.cpp:61
bool useFullDateFormat_
Definition: SgLogger.h:177
bool isNeedTimeMark_
Definition: SgLogger.h:176
QList< QString * > spool_
Definition: SgLogger.h:181
virtual void startUp()
Definition: SgLogger.cpp:74
double facilitiesSerialNumber_
Definition: SgLogger.h:184
bool isStoreInFile_
Definition: SgLogger.h:175
quint32 logFacilities_[4]
Definition: SgLogger.h:182
int capacity_
Definition: SgLogger.h:183
bool isEligible(LogLevel lvl, quint32 f) const
Definition: SgLogger.h:151
void detachSupplementLog(const QString &name)
Definition: SgLogger.cpp:187
void rmLogFile()
Definition: SgLogger.cpp:148
@ F_YYYYMMDDHHMMSSSS
Long verbose: Fri, the 2nd of Apr, 2010; 17hr 02min 43.6400sec.
Definition: SgMJD.h:67
@ F_HHMMSS
Just time: 17:02:43.6.
Definition: SgMJD.h:96
QString toString(Format format=F_Verbose) const
Definition: SgMJD.cpp:1007
static SgMJD currentMJD()
Definition: SgMJD.cpp:118