OpcUaCanOpen
CANopen OPC-UA server
LogRecord.cpp
Go to the documentation of this file.
1 /* © Copyright CERN, 2015. All rights not expressly granted are reserved.
2  * LogRecord.cpp
3  *
4  * Created on: Aug 18, 2015
5  * Author: Benjamin Farnham <benjamin.farnham@cern.ch>
6  *
7  * This file is part of Quasar.
8  *
9  * Quasar is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public Licence as published by
11  * the Free Software Foundation, either version 3 of the Licence.
12  *
13  * Quasar 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 Lesser General Public Licence for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with Quasar. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "LogRecord.h"
23 #include "LogItInstance.h"
24 #include "LogIt.h"
25 
32 #include <boost/date_time/posix_time/posix_time.hpp>
33 #include <boost/date_time/posix_time/posix_time_io.hpp>
34 #include <boost/format.hpp>
35 
36 using boost::posix_time::time_facet;
37 using boost::posix_time::microsec_clock;
38 
39 using std::string;
40 
41 const string g_sTimestampFormat = "%Y-%m-%d %H:%M.%s";
42 
43 LogRecord::LogRecord(const string& file, const int& line, const Log::LOG_LEVEL& level)
44 {
45  initializeStream(file, line, level)<<"] ";
46 }
47 
48 LogRecord::LogRecord(const string& file, const int& line, const Log::LOG_LEVEL& level, const Log::LogComponentHandle& componentHandle)
49 {
50  initializeStream(file, line, level)<<", "<<Log::getComponentName(componentHandle)<<"] ";
51 }
52 
53 LogRecord::LogRecord(const std::string& file, const int& line, const Log::LOG_LEVEL& level, const std::string& componentName)
54 {
55  initializeStream(file, line, level)<<", "<<componentName<<"] ";
56 }
57 
59 {
60  m_stream.flush();
62 }
63 
64 std::ostringstream& LogRecord::initializeStream(const string& file, const int& line, const Log::LOG_LEVEL& level)
65 {
66  m_stream.imbue(std::locale(m_stream.getloc(), new time_facet(g_sTimestampFormat.c_str())));
67  m_stream << microsec_clock::local_time() << " ["<<stripDirectory(file)<<":"<<line<<", "<<Log::logLevelToString(level);
68  return m_stream;
69 }
70 
71 std::ostream& LogRecord::getStream()
72 {
73  return m_stream;
74 }
75 
76 const string LogRecord::stripDirectory(const string& file)
77 {
78  const size_t trailingSlashPosition = file.find_last_of("/\\");
79 
80  if(trailingSlashPosition == std::string::npos) return file;
81 
82  if(trailingSlashPosition+1 >= file.length()) return file;
83 
84  return file.substr(trailingSlashPosition+1);
85 }