API
 
Loading...
Searching...
No Matches
logMap.cpp
Go to the documentation of this file.
1/** \file logMap.cpp
2 * \brief Declares and defines the logMap class and related classes.
3 * \author Jared R. Males (jaredmales@gmail.com)
4 *
5 * \ingroup logger_files
6 *
7 */
8
9#include "logMap.hpp"
10
11#include <sys/stat.h>
12#include <fcntl.h>
13#include <unistd.h>
14
15#include <filesystem>
16
17#include <mx/mxException.hpp>
18
19#include "../common/exceptions.hpp"
20
21using namespace flatlogs;
22
23namespace MagAOX
24{
25namespace logger
26{
27
29{
30 int fd = open( lfn.fullName().c_str(), O_RDONLY );
31
32 off_t fsz = mx::ioutils::fileSize( fd );
33
34 std::vector<char> memory( fsz );
35
36 ssize_t nrd = read( fd, memory.data(), memory.size() );
37
38 close( fd );
39
40 if( nrd != fsz )
41 {
42 std::cerr << __FILE__ << " " << __LINE__ << " logInMemory::loadFile(" << lfn.fullName()
43 << ") did not read all bytes\n";
44 return -1;
45 }
46
47 flatlogs::timespecX startTime = logHeader::timespec( memory.data() );
48
49 size_t st = 0;
50 size_t ed = logHeader::totalSize( memory.data() );
51 st = ed;
52
53 while( st < memory.size() )
54 {
55 ed = logHeader::totalSize( memory.data() + st );
56 st = st + ed;
57 }
58
59 if( st != memory.size() )
60 {
61 std::cerr << __FILE__ << " " << __LINE__ << " Possibly corrupt logfile.\n";
62 return -1;
63 }
64
65 st -= ed;
66
67 flatlogs::timespecX endTime = logHeader::timespec( memory.data() + st );
68
69 if( m_memory.size() == 0 )
70 {
71 m_memory.swap( memory );
72 m_startTime = startTime;
73 m_endTime = endTime;
74
75 std::string timestamp;
76 timespec ts{ endTime.time_s, endTime.time_ns };
77 mx::sys::timeStamp( timestamp, ts );
78
79#ifdef DEBUG
80 std::cerr << __FILE__ << " " << __LINE__ << " loading: " << lfn.fullName() << " " << timestamp << "\n";
81#endif
82
83 return 0;
84 }
85
86 if( startTime < m_startTime )
87 {
88
89 if( endTime >= m_startTime )
90 {
91 std::cerr << __FILE__ << " " << __LINE__ << " overlapping log files!\n";
92 return -1;
93 }
94
95 m_memory.insert( m_memory.begin(), memory.begin(), memory.end() );
96 m_startTime = startTime;
97 std::cerr << __FILE__ << " " << __LINE__ << " added before!\n";
98 return 0;
99 }
100
101 if( startTime > m_endTime )
102 {
103#ifdef DEBUG
104 std::cerr << __FILE__ << " " << __LINE__ << " gonna append\n";
105#endif
106
107 // m_memory.insert( m_memory.end(), memory.begin(), memory.end() );
108 m_endTime = endTime;
109
110#ifdef DEBUG
111 std::cerr << __FILE__ << " " << __LINE__ << " added after!\n";
112#endif
113
114 return 0;
115 }
116
117 std::cerr << __FILE__ << " " << __LINE__ << " Need to implement insert in the middle!\n";
118 std::cerr << m_startTime.time_s << " " << m_startTime.time_ns << "\n";
119 std::cerr << startTime.time_s << " " << startTime.time_ns << "\n";
120
121 return -1;
122}
123
124
125template class logMap<XWC_DEFAULT_VERBOSITY>;
126
127} // namespace logger
128} // namespace MagAOX
Organize and analyze the name of a standard file name.
mx::error_t fullName(const std::string &fullName)
Sets the full name.
static size_t totalSize(bufferPtrT &logBuffer)
Get the total size of the log entry, including the message buffer.
static int timespec(bufferPtrT &logBuffer, const timespecX &ts)
Set the timespec of a log entry.
Declares and defines the logMap class and related classes.
Definition dm.hpp:28
flatlogs::timespecX m_startTime
Definition logMap.hpp:42
int loadFile(file::stdFileName< verboseT > const &lfn)
Definition logMap.cpp:28
std::vector< char > m_memory
The buffer holding the log data.
Definition logMap.hpp:40
flatlogs::timespecX m_endTime
Definition logMap.hpp:43
Map of log entries by application name, mapping both to files and to loaded buffers.
Definition logMap.hpp:51
A fixed-width timespec structure.
Definition timespecX.hpp:35
nanosecT time_ns
Nanoseconds.
Definition timespecX.hpp:37
secT time_s
Time since the Unix epoch.
Definition timespecX.hpp:36