00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: MixedLogFormat.cpp 1230 2014-11-16 02:32:01Z sella $";
00010
00011 #include "MixedLogFormat.h"
00012 #include "Log.h"
00013 #include "../Exception.h"
00014
00015 #include <sys/time.h>
00016 #include <string.h>
00017 #include <stdio.h>
00018
00019 #include <string>
00020
00021 #define BUFFER_SIZE (64 * 1024)
00022
00023 using namespace sella::util;
00024
00025 MixedLogFormat::MixedLogFormat(int threshold) throw () : threshold(threshold) { }
00026
00027 MixedLogFormat* MixedLogFormat::clone(void) const throw () {
00028 return new MixedLogFormat(*this);
00029 }
00030
00031 const std::string MixedLogFormat::format(int priority, const char *stamp, const char *func, const char *file, int line, const sella::Exception &e, const char *message) throw () {
00032 char buffer[BUFFER_SIZE];
00033
00034 if (priority >= threshold) {
00035 if (message == NULL || message[0] == '\0') {
00036 snprintf(buffer, sizeof(buffer), "%s [%s:%d %s]", e.what(), e.getFile().c_str(), e.getLine(), e.getFunction().c_str());
00037 } else {
00038 snprintf(buffer, sizeof(buffer), "%s - %s [%s:%d %s]", message, e.what(), e.getFile().c_str(), e.getLine(), e.getFunction().c_str());
00039 }
00040 } else {
00041 if (message == NULL || message[0] == '\0') {
00042 snprintf(buffer, sizeof(buffer), "%s [%s:%d %s] <- [%s:%d %s]", e.what(), file, line, func, e.getFile().c_str(), e.getLine(), e.getFunction().c_str());
00043 } else {
00044 snprintf(buffer, sizeof(buffer), "%s - %s [%s:%d %s] <- [%s:%d %s]", message, e.what(), file, line, func, e.getFile().c_str(), e.getLine(), e.getFunction().c_str());
00045 }
00046 }
00047
00048 return std::string(buffer);
00049 }
00050
00051 const std::string MixedLogFormat::format(int priority, const char *stamp, const char *func, const char *file, int line, const char *message) throw () {
00052 char buffer[BUFFER_SIZE];
00053
00054 if (priority >= threshold) {
00055 snprintf(buffer, sizeof(buffer), "%s", message);
00056 } else {
00057 snprintf(buffer, sizeof(buffer), "%s [%s:%d %s]", message, file, line, func);
00058 }
00059
00060 return std::string(buffer);
00061 }
00062
00063 const std::string MixedLogFormat::format(int priority, const char *stamp, const char *func, const char *file, int line, const char *option, const char *message) throw () {
00064 char buffer[BUFFER_SIZE];
00065
00066 if (priority >= threshold) {
00067 snprintf(buffer, sizeof(buffer), "%s {%s}", message, option);
00068 } else {
00069 snprintf(buffer, sizeof(buffer), "%s [%s:%d %s] {%s}", message, file, line, func, option);
00070
00071 }
00072
00073 return std::string(buffer);
00074 }
00075
00076
00077
00078