libassa  3.5.1
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
ASSA::FileLogger Class Reference

#include <FileLogger.h>

Inheritance diagram for ASSA::FileLogger:
ASSA::Logger_Impl

Public Member Functions

 FileLogger ()
 
virtual int log_open (const char *logfname_, u_long groups_, u_long maxsize_=10485760)
 Open File Logger. More...
 
virtual int log_close (void)
 
virtual void log_resync (void)
 
virtual int log_msg (Group g_, size_t indent_level_, const string &func_name_, size_t expected_sz_, const char *fmt_, va_list)
 If output string is longer then LOGGER_MAXLINE-1, it is truncated to that size. More...
 
virtual int log_func (Group g_, size_t indent_level_, const string &func_name_, marker_t type_)
 
int log_raw_msg (const string &msg_)
 Log message as it is (raw) without indentation or timestamping, but still perform byte counting and the logic associated with it. More...
 
void dump (void)
 
- Public Member Functions inherited from ASSA::Logger_Impl
 Logger_Impl ()
 
virtual ~Logger_Impl ()
 
void enable_group (Group g_)
 
void disable_group (Group g_)
 
void enable_groups (u_long g_)
 
void disable_groups (u_long g_)
 
void enable_all_groups (void)
 
void disable_all_groups (void)
 
bool group_enabled (Group g_) const
 
void enable_timestamp (void)
 
void disable_timestamp (void)
 
bool timestamp_enabled (void) const
 
void set_timezone (int zone_)
 
void set_indent_step (u_short step_)
 
u_short get_indent_step (void) const
 
virtual int log_open (u_long groups_)
 Open StdErr Logger. More...
 
virtual int log_open (const char *appname_, const char *logfname_, u_long groups_, u_long maxsize_, Reactor *reactor_)
 Open connection with Log Server. More...
 

Private Types

enum  state_t { opened , closed }
 

Private Member Functions

 FileLogger (const FileLogger &)
 
FileLoggeroperator= (const FileLogger &)
 
int handle_rollover ()
 

Private Attributes

std::ofstream m_sink
 
u_long m_maxsize
 
state_t m_state
 
u_long m_bytecount
 

Additional Inherited Members

- Static Public Attributes inherited from ASSA::Logger_Impl
static const unsigned int LOGGER_MAXLINE = 6660
 Maximum length of the formatted message. More...
 
- Protected Member Functions inherited from ASSA::Logger_Impl
virtual u_short add_timestamp (ostream &sink_)
 
virtual u_short indent_func_name (ostream &sink_, const string &funcname_, size_t indent_level_, marker_t type_)
 
char * format_msg (size_t expected_sz_, const char *fmt_, va_list vap_, bool &release_)
 Format and put the message in the buffer. More...
 
- Protected Attributes inherited from ASSA::Logger_Impl
u_short m_indent_step
 Indentation step. More...
 
u_long m_groups
 Enabled groups. More...
 
string m_logfname
 Log file name. More...
 
bool m_tmflg
 Timestamp on/off flag. More...
 
int m_tz
 Timezone: 0-GMT, 1-Local. More...
 
- Static Protected Attributes inherited from ASSA::Logger_Impl
static char m_msgbuf [LOGGER_MAXLINE]
 Static buffer for formatted message. More...
 

Detailed Description

Definition at line 30 of file FileLogger.h.

Member Enumeration Documentation

◆ state_t

Enumerator
opened 
closed 

Definition at line 60 of file FileLogger.h.

Constructor & Destructor Documentation

◆ FileLogger() [1/2]

ASSA::FileLogger::FileLogger ( )
inline

Definition at line 74 of file FileLogger.h.

76  : m_maxsize (1048576),
77  m_state (closed),
78  m_bytecount (0)
79 {
80  /*--- empty ---*/
81 }
u_long m_maxsize
Definition: FileLogger.h:69
state_t m_state
Definition: FileLogger.h:70
u_long m_bytecount
Definition: FileLogger.h:71

◆ FileLogger() [2/2]

ASSA::FileLogger::FileLogger ( const FileLogger )
private

Member Function Documentation

◆ dump()

void FileLogger::dump ( void  )

Definition at line 234 of file FileLogger.cpp.

236 {
237 #ifdef BUG_HUNTING
238  if (m_state == opened) {
239  m_sink << "m_logfname = \"" << m_logfname << "\"\n"
240  << "m_groups = 0x";
241  char oldfill = m_sink.fill ('0');
242  m_sink << std::setw(8) << std::hex << m_groups << '\n' << std::dec;
243  m_sink.fill (oldfill);
244  m_sink << "m_indent_step = " << m_indent_step << '\n'
245  << "m_tmflg = " << m_tmflg << '\n'
246  << "m_maxsize = " << m_maxsize << '\n'
247  << "m_state = opened\n"
248  << "m_bytecount = " << m_bytecount << std::endl;
249  }
250 #endif
251 }
std::ofstream m_sink
Definition: FileLogger.h:68
u_short m_indent_step
Indentation step.
Definition: Logger_Impl.h:236
u_long m_groups
Enabled groups.
Definition: Logger_Impl.h:239
string m_logfname
Log file name.
Definition: Logger_Impl.h:242
bool m_tmflg
Timestamp on/off flag.
Definition: Logger_Impl.h:245
Socket & endl(Socket &os_)
endl manipulator.
Definition: Socket.h:602

References ASSA::endl(), m_bytecount, ASSA::Logger_Impl::m_groups, ASSA::Logger_Impl::m_indent_step, ASSA::Logger_Impl::m_logfname, m_maxsize, m_sink, m_state, ASSA::Logger_Impl::m_tmflg, and opened.

◆ handle_rollover()

int FileLogger::handle_rollover ( )
private

Definition at line 198 of file FileLogger.cpp.

200 {
201  if (m_bytecount >= m_maxsize) {
202  struct stat fst;
203  if (::stat (m_logfname.c_str(), &fst) == 0) {
204  if (S_ISREG (fst.st_mode)) {
205  m_sink << "\nReached maximum allowable size\n"
206  << "m_bytecount = " << m_bytecount
207  << ", m_maxsize = " << m_maxsize << std::endl;
208  m_sink.close ();
209  m_state = closed;
210  m_bytecount = 0;
211 
212  string newname = m_logfname + ".0";
213  unlink (newname.c_str ());
214  rename (m_logfname.c_str (), newname.c_str ());
215  m_sink.open (m_logfname.c_str (),
216  std::ios::app | std::ios::out);
217  if (!m_sink) {
218  return -1;
219  }
220  m_state = opened;
221  }
222  else if (S_ISCHR (fst.st_mode)) { // It is /dev/null
223  m_bytecount = 0;
224  }
225  else {
226  Assure_exit (1);
227  }
228  }
229  }
230  return 0;
231 }
#define Assure_exit(exp_)
Macro that makes program exit if assert fails.
Definition: Assure.h:39

References Assure_exit, closed, ASSA::endl(), m_bytecount, ASSA::Logger_Impl::m_logfname, m_maxsize, m_sink, m_state, and opened.

Referenced by log_func(), log_msg(), and log_raw_msg().

◆ log_close()

int FileLogger::log_close ( void  )
virtual

Implements ASSA::Logger_Impl.

Definition at line 69 of file FileLogger.cpp.

71 {
72  if (m_state != closed) {
73  m_sink << std::flush;
74  m_sink.close ();
75  m_state = closed;
76 
77  if (m_groups == 0) {
78  ::unlink (m_logfname.c_str ());
79  }
80  m_logfname.empty ();
81  m_maxsize = 0;
82  m_bytecount = 0;
83  }
84  return 0;
85 }
Socket & flush(Socket &os_)
flush manipulator.
Definition: Socket.h:587

References closed, ASSA::flush(), m_bytecount, ASSA::Logger_Impl::m_groups, ASSA::Logger_Impl::m_logfname, m_maxsize, m_sink, and m_state.

◆ log_func()

int FileLogger::log_func ( Group  g_,
size_t  indent_level_,
const string &  func_name_,
marker_t  type_ 
)
virtual

Implements ASSA::Logger_Impl.

Definition at line 161 of file FileLogger.cpp.

164 {
165  if (m_state == closed) {
166  errno = EPERM;
167  return -1;
168  }
169 
170  if (! group_enabled (g_)) {
171  return 0;
172  }
173 
175  m_bytecount += indent_func_name (m_sink, func_name_, indent_level_, type_);
176  m_sink << ((type_ == FUNC_ENTRY) ? "---v---\n" : "---^---\n") << std::flush;
177  m_bytecount += ::strlen ("---v---\n");
178 
179  return handle_rollover ();
180 }
virtual u_short indent_func_name(ostream &sink_, const string &funcname_, size_t indent_level_, marker_t type_)
Definition: Logger_Impl.cpp:54
bool group_enabled(Group g_) const
Definition: Logger_Impl.h:164
virtual u_short add_timestamp(ostream &sink_)
Definition: Logger_Impl.cpp:35
@ FUNC_ENTRY
Definition: LogMask.h:69

References ASSA::Logger_Impl::add_timestamp(), closed, ASSA::flush(), ASSA::FUNC_ENTRY, ASSA::Logger_Impl::group_enabled(), handle_rollover(), ASSA::Logger_Impl::indent_func_name(), m_bytecount, m_sink, and m_state.

◆ log_msg()

int FileLogger::log_msg ( Group  g_,
size_t  indent_level_,
const string &  func_name_,
size_t  expected_sz_,
const char *  fmt_,
va_list  msg_list_ 
)
virtual

If output string is longer then LOGGER_MAXLINE-1, it is truncated to that size.

From printf(3) manpage:

"Upon successful return, these functions return the number of characters printed (not including the trailing '\0' used to end output to strings).

If the output was truncated due to this limit then the return value is the number of characters (not including the trailing '\0') which would have been written to the final string if enough space had been available.

Thus, a return value of size or more means that the output was truncated.

If an output error is encountered, a negative value is returned."

In other words, if you have attempted to write more then buffer can hold, the output is truncated by buffer size - 1, and the return value would be the number of bytes you have tried to write to the buffer.

If buffer size is 256, and you tried to write 340 bytes to it, the first 255 bytes are written to the buffer, and 340 is returns as the return value.

Implements ASSA::Logger_Impl.

Definition at line 116 of file FileLogger.cpp.

123 {
124 // std::cout << "FileLogger::log_msg() enter\n"
125 // << "group__=0x" << std::setw(8) << std::hex << (u_long)g_ << "\n";
126 
127  if (m_state == closed) {
128 // std::cout << "FileLogger::log_msg() sink closed!\n";
129  errno = EPERM;
130  return -1;
131  }
132 
133  if (! group_enabled (g_)) {
134 // std::cout << "FileLogger::log_msg() group is not enabled!\n"
135 // << "m_groups=0x"
136 // << std::setw(8) << std::hex << m_groups << "\n";
137  return 0;
138  }
139 
141  m_bytecount += indent_func_name (m_sink, func_name_,indent_level_,FUNC_MSG);
142 
143  bool release = false;
144  char* msgbuf_ptr = format_msg (expected_sz_, fmt_, msg_list_, release);
145  if (msgbuf_ptr == NULL) {
146 // std::cout << "FileLogger::log_msg() call to format_msg() failed!"
147 // << " fmt_= \"" << fmt_ << "\"\n" << std::flush;
148  return -1; // failed to format
149  }
150  m_sink << msgbuf_ptr << std::flush;
151  m_bytecount += strlen (msgbuf_ptr);
152 
153  if (release) {
154  delete [] msgbuf_ptr;
155  }
156 
157  return handle_rollover ();
158 }
char * format_msg(size_t expected_sz_, const char *fmt_, va_list vap_, bool &release_)
Format and put the message in the buffer.
Definition: Logger_Impl.cpp:86
@ FUNC_MSG
Definition: LogMask.h:68

References ASSA::Logger_Impl::add_timestamp(), closed, ASSA::flush(), ASSA::Logger_Impl::format_msg(), ASSA::FUNC_MSG, ASSA::Logger_Impl::group_enabled(), handle_rollover(), ASSA::Logger_Impl::indent_func_name(), m_bytecount, m_sink, and m_state.

◆ log_open()

int FileLogger::log_open ( const char *  logfname_,
u_long  groups_,
u_long  maxsize_ = 10485760 
)
virtual

Open File Logger.

Reimplemented from ASSA::Logger_Impl.

Definition at line 32 of file FileLogger.cpp.

34 {
35 // std::cout << "Enter: FileLogger::log_open(fname=\""
36 // << logfname_ << "\""
37 // << " groups=0x" << std::setw(8) << std::hex << groups_
38 // << " maxsize=" << std::dec << maxsize_ << ")\n";
39 
40  if (logfname_ == NULL || maxsize_ <= 0) {
41 // std::cout << "FileLogger::log_open() failed 1\n";
42  errno = EINVAL;
43  return -1;
44  }
45 
46  if (m_state == opened) {
47 // std::cout << "FileLogger::log_open() already open\n";
48  errno = EEXIST;
49  return -1;
50  }
51 
52  m_logfname = logfname_;
53  m_groups = groups_;
54  m_maxsize = maxsize_;
55 
56  m_sink.open (m_logfname.c_str (), std::ios::out | std::ios::app);
57 
58  if (!m_sink) {
59 // std::cout << "FileLogger::log_open() failed to open()!\n";
60  return -1;
61  }
62 // std::cout << "Success on FileLogger::log_open()\n";
63 
64  m_state = opened;
65  return 0;
66 }

References ASSA::Logger_Impl::m_groups, ASSA::Logger_Impl::m_logfname, m_maxsize, m_sink, m_state, and opened.

◆ log_raw_msg()

int FileLogger::log_raw_msg ( const string &  msg_)

Log message as it is (raw) without indentation or timestamping, but still perform byte counting and the logic associated with it.

Definition at line 183 of file FileLogger.cpp.

185 {
186  if (m_state == closed) {
187  errno = EPERM;
188  return -1;
189  }
190 
191  m_sink << msg_ << std::flush;
192  m_bytecount += msg_.length ();
193 
194  return handle_rollover ();
195 }

References closed, ASSA::flush(), handle_rollover(), m_bytecount, m_sink, and m_state.

◆ log_resync()

void ASSA::FileLogger::log_resync ( void  )
inlinevirtual

Reimplemented from ASSA::Logger_Impl.

Definition at line 84 of file FileLogger.h.

86 {
87  m_sink << std::flush;
88 }

References ASSA::flush(), and m_sink.

◆ operator=()

FileLogger& ASSA::FileLogger::operator= ( const FileLogger )
private

Member Data Documentation

◆ m_bytecount

u_long ASSA::FileLogger::m_bytecount
private

Definition at line 71 of file FileLogger.h.

Referenced by dump(), handle_rollover(), log_close(), log_func(), log_msg(), and log_raw_msg().

◆ m_maxsize

u_long ASSA::FileLogger::m_maxsize
private

Definition at line 69 of file FileLogger.h.

Referenced by dump(), handle_rollover(), log_close(), and log_open().

◆ m_sink

std::ofstream ASSA::FileLogger::m_sink
private

◆ m_state

state_t ASSA::FileLogger::m_state
private

Definition at line 70 of file FileLogger.h.

Referenced by dump(), handle_rollover(), log_close(), log_func(), log_msg(), log_open(), and log_raw_msg().


The documentation for this class was generated from the following files: