[med-svn] [aghermann] 85/85: borrow log-facility from cnrun, let it lie spare for now
andrei zavada
hmmr-guest at alioth.debian.org
Thu Sep 26 23:46:38 UTC 2013
This is an automated email from the git hooks/post-receive script.
hmmr-guest pushed a commit to branch master
in repository aghermann.
commit d36d90cdd43d0c88d9c72e2da014215129f6bec0
Author: Andrei Zavada <johnhommer at gmail.com>
Date: Fri Sep 27 02:45:05 2013 +0300
borrow log-facility from cnrun, let it lie spare for now
---
upstream/src/common/log-facility.cc | 145 +++++++++++++++++++++++++++++++++++
upstream/src/common/log-facility.hh | 75 ++++++++++++++++++
2 files changed, 220 insertions(+)
diff --git a/upstream/src/common/log-facility.cc b/upstream/src/common/log-facility.cc
new file mode 100644
index 0000000..be7fc86
--- /dev/null
+++ b/upstream/src/common/log-facility.cc
@@ -0,0 +1,145 @@
+/*
+ * Author: Andrei Zavada <johnhommer at gmail.com>
+ *
+ * License: GPL-2+
+ *
+ * Initial version: 2009-06-28
+ *
+ */
+
+#include <sys/time.h>
+#include <cstdarg>
+#include <cstring>
+#include <cmath>
+#include <fstream>
+
+#include "config.h"
+
+#include "log-facility.hh"
+
+using namespace std;
+
+
+Stilton::CLogFacility::
+CLogFacility( const char *log_fname,
+ int inlog_threshold,
+ int instdout_tee_threshold,
+ unsigned short insec_dec_place,
+ int bits,
+ size_t buf_size)
+ : status (bits),
+ log_threshold (inlog_threshold),
+ stdout_tee_threshold (instdout_tee_threshold),
+ sec_dec_places (insec_dec_place)
+{
+ _line_buf = new char[_buf_size = buf_size];
+ if ( log_fname && strlen(log_fname) ) {
+ _log_fname = string(log_fname);
+ _log_strm.open( log_fname);
+ unitbuf( _log_strm);
+ }
+}
+
+Stilton::CLogFacility::
+~CLogFacility()
+{
+ if ( _log_fname.size() )
+ _log_strm.close();
+ delete[] _line_buf;
+}
+
+
+void
+Stilton::CLogFacility::
+msg( int vrb, const char *client_name, const char* fmt, ...)
+{
+ va_list ap;
+ va_start (ap, fmt);
+ msgv( vrb, client_name, fmt, ap);
+ va_end (ap);
+}
+
+
+void
+Stilton::CLogFacility::
+msgv( int vrb, const char *client_name, const char* fmt, va_list ap)
+{
+// if ( status & STILTON_LOG_NOLOCK )
+// boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> L( _log_lock);
+
+ if ( log_threshold < vrb && stdout_tee_threshold < vrb )
+ return;
+
+ char timestampbuf[32];
+ time_t timestamp; time( ×tamp);
+ struct timeval tp; gettimeofday( &tp, nullptr);
+ strftime( timestampbuf, 31, "%F %T", localtime( ×tamp));
+ char secfracbuf[sec_dec_places+3];
+ snprintf( secfracbuf, sec_dec_places+2, ".%0*u", sec_dec_places,
+ (unsigned)round( tp.tv_usec / pow( 10., 6-sec_dec_places-1)));
+
+ vsnprintf( _line_buf, _buf_size, fmt, ap);
+
+ char *line = strtok( _line_buf, "\n");
+ do {
+ if ( vrb < 0 )
+ printf( "%s%sError: %s\n", client_name ? client_name : "", (client_name && strlen(client_name)) ? ": " : "", line);
+ else if ( stdout_tee_threshold >= vrb )
+ printf( "%s%s%s\n", client_name ? client_name : "", (client_name && strlen(client_name)) ? ": " : "", line);
+
+ if ( log_threshold >= vrb && _log_fname.size() )
+ _log_strm << timestampbuf << (sec_dec_places > 0 ? secfracbuf : "") << ' '
+ << client_name << ": "
+ << (vrb < 0 ? "Error: " : "") << line << endl;
+ } while ( (line = strtok( nullptr, "\n")) );
+
+ if ( _log_fname.size() )
+ _log_strm.flush();
+}
+
+
+
+// a one-liner, possibly unterminated by \n
+void
+Stilton::CLogFacility::
+msg_( int vrb, const char *client_name, const char* fmt, ...)
+{
+ va_list ap;
+ va_start (ap, fmt);
+ msgv_( vrb, client_name, fmt, ap);
+ va_end (ap);
+}
+
+void
+Stilton::CLogFacility::
+msgv_( int vrb, const char *client_name, const char* fmt, va_list ap)
+{
+// if ( status & STILTON_LOG_NOLOCK )
+// boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> L( _log_lock);
+
+ if ( log_threshold < vrb && stdout_tee_threshold < vrb )
+ return;
+
+ char timestampbuf[32];
+ time_t timestamp; time( ×tamp);
+ struct timeval tp; gettimeofday( &tp, nullptr);
+ strftime( timestampbuf, 31, "%F %T", localtime( ×tamp));
+ char secfracbuf[sec_dec_places+3];
+ snprintf( secfracbuf, sec_dec_places+2, ".%0*u", sec_dec_places,
+ (unsigned)round( tp.tv_usec / pow( 10., 6-sec_dec_places-1)));
+
+ vsnprintf( _line_buf, _buf_size, fmt, ap);
+
+ if ( vrb < 0 )
+ printf( "%s%sError: %s", client_name ? client_name : "", (client_name && strlen(client_name)) ? ": " : "", _line_buf);
+ else if ( stdout_tee_threshold >= vrb )
+ printf( "%s%s%s", client_name ? client_name : "", (client_name && strlen(client_name)) ? ": " : "", _line_buf);
+
+ if ( log_threshold >= vrb && _log_fname.size() )
+ _log_strm << timestampbuf << (sec_dec_places > 0 ? secfracbuf : "") << ' '
+ << client_name << ": "
+ << (vrb < 0 ? "Error: " : "") << _line_buf << endl;
+}
+
+
+// eof
diff --git a/upstream/src/common/log-facility.hh b/upstream/src/common/log-facility.hh
new file mode 100644
index 0000000..e48c807
--- /dev/null
+++ b/upstream/src/common/log-facility.hh
@@ -0,0 +1,75 @@
+/*
+ * Author: Andrei Zavada <johnhommer at gmail.com>
+ *
+ * License: GPL-2+
+ *
+ * Initial version: 2009-06-28
+ *
+ */
+
+#ifndef STILTON_LOG_FACILITY_H
+#define STILTON_LOG_FACILITY_H
+
+#include <cstdarg>
+#include <fstream>
+
+//#include <boost/interprocess/sync/interprocess_mutex.hpp>
+//#include <boost/interprocess/sync/interprocess_condition.hpp>
+
+
+namespace Stilton {
+
+using namespace std;
+using namespace Stilton;
+
+
+// bitwise OR this with CLogFacility::status to prevent locking even if supported
+#define STILTON_LOG_NOLOCK 1
+
+class CLogFacility {
+
+ public:
+ CLogFacility( const char *log_fname,
+ int log_threshold = 4,
+ int stdout_tee_threshold = 2,
+ unsigned short sec_dec_places = 2,
+ int bits = 0,
+ size_t buf_size = 2048);
+
+ int status;
+
+ int log_threshold,
+ stdout_tee_threshold;
+ unsigned short
+ sec_dec_places;
+ // full-featured; always terminating lines with a \n
+ void msg( int vrb, const char *client_name, const char* fmt, ...);
+ void msgv( int vrb, const char *client_name, const char* fmt, va_list);
+ // raw output: no parsing, not timestamping each line
+ void msg_( int vrb, const char *client_name, const char* fmt, ...);
+ void msgv_( int vrb, const char *client_name, const char* fmt, va_list);
+
+ const char *log_fname() const
+ { return _log_fname.c_str(); }
+ size_t buf_size() const
+ { return _buf_size; }
+
+ ~CLogFacility();
+
+ private:
+ string _log_fname;
+ size_t _buf_size;
+ ofstream
+ _log_strm;
+ char
+ *_line_buf;
+// boost::interprocess::interprocess_mutex
+// _log_lock;
+};
+
+
+}
+
+#endif
+
+// EOF
--
Alioth's /git/debian-med/git-commit-notice on /srv/git.debian.org/git/debian-med/aghermann.git
More information about the debian-med-commit
mailing list