Skip to content

Commit b61c868

Browse files
committed
added log prefix to each SQL thread
1 parent 547ba49 commit b61c868

File tree

3 files changed

+61
-28
lines changed

3 files changed

+61
-28
lines changed

src/InfoLoggerDispatch.cxx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
// class InfoLoggerDispatch implementation
1616
///////////////////////////////////////////
1717

18-
InfoLoggerDispatch::InfoLoggerDispatch(ConfigInfoLoggerServer* vConfig, SimpleLog* vLog)
18+
InfoLoggerDispatch::InfoLoggerDispatch(ConfigInfoLoggerServer* vConfig, SimpleLog* vLog, std::string vLogPrefix)
1919
{
20+
logPrefix = vLogPrefix;
2021
input = std::make_unique<AliceO2::Common::Fifo<std::shared_ptr<InfoLoggerMessageList>>>(vConfig->dbDispatchQueueSize);
2122
if (vLog != NULL) {
2223
theLog = vLog;
@@ -87,6 +88,33 @@ int InfoLoggerDispatchPrint::customMessageProcess(std::shared_ptr<InfoLoggerMess
8788
return 0;
8889
}
8990

91+
void InfoLoggerDispatch::logInfo(const char* message, ...) {
92+
char buffer[1024] = "";
93+
va_list ap;
94+
va_start(ap, message);
95+
vsnprintf(buffer,sizeof(buffer), message, ap);
96+
va_end(ap);
97+
theLog->info("%s%s", logPrefix.c_str(), buffer);
98+
}
99+
100+
void InfoLoggerDispatch::logError(const char* message, ...) {
101+
char buffer[1024] = "";
102+
va_list ap;
103+
va_start(ap, message);
104+
vsnprintf(buffer,sizeof(buffer), message, ap);
105+
va_end(ap);
106+
theLog->error("%s%s", logPrefix.c_str(), buffer);
107+
}
108+
109+
void InfoLoggerDispatch::logWarning(const char* message, ...) {
110+
char buffer[1024] = "";
111+
va_list ap;
112+
va_start(ap, message);
113+
vsnprintf(buffer,sizeof(buffer), message, ap);
114+
va_end(ap);
115+
theLog->warning("%s%s", logPrefix.c_str(), buffer);
116+
}
117+
90118
/////////////////////////////////////////////////
91119
// end of class InfoLoggerDispatch implementation
92120
/////////////////////////////////////////////////

src/InfoLoggerDispatch.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class InfoLoggerDispatch
3131
{
3232

3333
public:
34-
InfoLoggerDispatch(ConfigInfoLoggerServer* theConfig = NULL, SimpleLog* theLog = NULL);
34+
InfoLoggerDispatch(ConfigInfoLoggerServer* theConfig = NULL, SimpleLog* theLog = NULL, std::string theLogPrefix = "");
3535
virtual ~InfoLoggerDispatch();
3636

3737
// todo: define settings: non-blocking, etc
@@ -46,11 +46,16 @@ class InfoLoggerDispatch
4646

4747
// virtual customDispatch...
4848

49+
void logInfo(const char* message, ...) __attribute__((format(printf, 2, 3)));
50+
void logError(const char* message, ...) __attribute__((format(printf, 2, 3)));
51+
void logWarning(const char* message, ...) __attribute__((format(printf, 2, 3)));
52+
4953
protected:
5054
std::unique_ptr<AliceO2::Common::Fifo<std::shared_ptr<InfoLoggerMessageList>>> input;
5155
std::unique_ptr<AliceO2::Common::Thread> dispatchThread;
5256
SimpleLog* theLog;
5357
SimpleLog defaultLog;
58+
std::string logPrefix; // a string appended to each log
5459

5560
ConfigInfoLoggerServer* theConfig;
5661

@@ -82,7 +87,7 @@ class InfoLoggerDispatchSQLImpl;
8287
class InfoLoggerDispatchSQL : public InfoLoggerDispatch
8388
{
8489
public:
85-
InfoLoggerDispatchSQL(ConfigInfoLoggerServer* theConfig, SimpleLog* theLog);
90+
InfoLoggerDispatchSQL(ConfigInfoLoggerServer* theConfig, SimpleLog* theLog, std::string prefix);
8691
~InfoLoggerDispatchSQL();
8792
int customMessageProcess(std::shared_ptr<InfoLoggerMessageList> msg);
8893
int customLoop();

src/InfoLoggerDispatchSQL.cxx

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ typedef bool my_bool;
2828
class InfoLoggerDispatchSQLImpl
2929
{
3030
public:
31-
SimpleLog* theLog; // handle for logging
3231
ConfigInfoLoggerServer* theConfig; // struct with config parameters
32+
InfoLoggerDispatchSQL* parent;
3333

3434
void start();
3535
void stop();
@@ -67,7 +67,7 @@ void InfoLoggerDispatchSQLImpl::start()
6767
{
6868

6969
// log DB params
70-
theLog->info("Using DB %s@%s:%s", theConfig->dbUser.c_str(), theConfig->dbHost.c_str(), theConfig->dbName.c_str());
70+
parent->logInfo("Using DB %s@%s:%s", theConfig->dbUser.c_str(), theConfig->dbHost.c_str(), theConfig->dbName.c_str());
7171

7272
// prepare insert query from 1st protocol definition
7373
// e.g. INSERT INTO messages(severity,level,timestamp,hostname,rolename,pid,username,system,facility,detector,partition,run,errcode,errLine,errsource) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) */
@@ -101,20 +101,20 @@ void InfoLoggerDispatchSQLImpl::start()
101101
}
102102
}
103103
sql_insert += ")";
104-
theLog->info("insert query = %s", sql_insert.c_str());
104+
parent->logInfo("insert query = %s", sql_insert.c_str());
105105
if (errLine) {
106-
theLog->error("Failed to initialize db query: error %d", errLine);
106+
parent->logError("Failed to initialize db query: error %d", errLine);
107107
}
108108

109109
// try to connect DB
110110
// done automatically in customloop
111111
}
112112

113-
InfoLoggerDispatchSQL::InfoLoggerDispatchSQL(ConfigInfoLoggerServer* config, SimpleLog* log) : InfoLoggerDispatch(config, log)
113+
InfoLoggerDispatchSQL::InfoLoggerDispatchSQL(ConfigInfoLoggerServer* config, SimpleLog* log, std::string prefix) : InfoLoggerDispatch(config, log, prefix)
114114
{
115115
dPtr = std::make_unique<InfoLoggerDispatchSQLImpl>();
116-
dPtr->theLog = log;
117116
dPtr->theConfig = config;
117+
dPtr->parent = this;
118118
dPtr->start();
119119

120120
// enable customloop callback
@@ -124,7 +124,7 @@ InfoLoggerDispatchSQL::InfoLoggerDispatchSQL(ConfigInfoLoggerServer* config, Sim
124124
void InfoLoggerDispatchSQLImpl::stop()
125125
{
126126
disconnectDB();
127-
theLog->info("DB thread insert count = %llu, delayed msg count = %llu, dropped msg count = %llu", insertCount, msgDelayedCount, msgDroppedCount);
127+
parent->logInfo("DB thread insert count = %llu, delayed msg count = %llu, dropped msg count = %llu", insertCount, msgDelayedCount, msgDroppedCount);
128128
}
129129

130130
InfoLoggerDispatchSQL::~InfoLoggerDispatchSQL()
@@ -156,18 +156,18 @@ int InfoLoggerDispatchSQLImpl::connectDB()
156156
// init mysql handle
157157
db = mysql_init(db);
158158
if (db == NULL) {
159-
theLog->error("mysql_init() failed");
159+
parent->logError("mysql_init() failed");
160160
return 1;
161161
}
162162
}
163-
163+
parent->logInfo("DB connecting: %s@%s:%s", theConfig->dbUser.c_str(), theConfig->dbHost.c_str(), theConfig->dbName.c_str());
164164
if (mysql_real_connect(db, theConfig->dbHost.c_str(), theConfig->dbUser.c_str(), theConfig->dbPassword.c_str(), theConfig->dbName.c_str(), 0, NULL, 0)) {
165-
theLog->info("DB connected");
165+
parent->logInfo("DB connected");
166166
dbIsConnected = 1;
167167
dbConnectTrials = 0;
168168
} else {
169169
if (dbConnectTrials == 0) { // log only first attempt
170-
theLog->error("DB connection failed: %s", mysql_error(db));
170+
parent->logError("DB connection failed: %s", mysql_error(db));
171171
}
172172
dbConnectTrials++;
173173
return 1;
@@ -176,13 +176,13 @@ int InfoLoggerDispatchSQLImpl::connectDB()
176176
// create prepared insert statement
177177
stmt = mysql_stmt_init(db);
178178
if (stmt == NULL) {
179-
theLog->error("mysql_stmt_init() failed: %s", mysql_error(db));
179+
parent->logError("mysql_stmt_init() failed: %s", mysql_error(db));
180180
disconnectDB();
181181
return -1;
182182
}
183183

184184
if (mysql_stmt_prepare(stmt, sql_insert.c_str(), sql_insert.length())) {
185-
theLog->error("mysql_stmt_prepare() failed: %s", mysql_error(db));
185+
parent->logError("mysql_stmt_prepare() failed: %s", mysql_error(db));
186186
disconnectDB();
187187
return -1;
188188
}
@@ -202,7 +202,7 @@ int InfoLoggerDispatchSQLImpl::connectDB()
202202
bind[i].buffer_type = MYSQL_TYPE_DOUBLE;
203203
break;
204204
default:
205-
theLog->error("undefined field type %d", protocols[0].fields[i].type);
205+
parent->logError("undefined field type %d", protocols[0].fields[i].type);
206206
errline = __LINE__;
207207
break;
208208
}
@@ -230,7 +230,7 @@ int InfoLoggerDispatchSQLImpl::disconnectDB()
230230
db = NULL;
231231
}
232232
if (dbIsConnected) {
233-
theLog->info("DB disconnected");
233+
parent->logInfo("DB disconnected");
234234
}
235235
dbIsConnected = 0;
236236
return 0;
@@ -247,11 +247,11 @@ int InfoLoggerDispatchSQLImpl::customLoop()
247247
if (commitNumberOfMsg) {
248248
if (commitTimer.isTimeout()) {
249249
if (mysql_query(db, "COMMIT")) {
250-
theLog->error("DB transaction commit failed: %s", mysql_error(db));
250+
parent->logError("DB transaction commit failed: %s", mysql_error(db));
251251
commitEnabled = 0;
252252
} else {
253253
if (commitDebug) {
254-
theLog->info("DB commit - %d msgs", commitNumberOfMsg);
254+
parent->logInfo("DB commit - %d msgs", commitNumberOfMsg);
255255
}
256256
}
257257
commitNumberOfMsg = 0;
@@ -299,8 +299,8 @@ int InfoLoggerDispatchSQLImpl::customMessageProcess(std::shared_ptr<InfoLoggerMe
299299
}
300300

301301
int msgLen = (int)strlen(message);
302-
theLog->error("Dropping message (%d bytes): %.*s%s", msgLen, maxLen, message, (msgLen > maxLen) ? "..." : "");
303-
theLog->error(" %s", logDetails.c_str());
302+
parent->logError("Dropping message (%d bytes): %.*s%s", msgLen, maxLen, message, (msgLen > maxLen) ? "..." : "");
303+
parent->logError(" %s", logDetails.c_str());
304304
msgDroppedCount++;
305305
return 0; // remove message from queue
306306
};
@@ -350,12 +350,12 @@ int InfoLoggerDispatchSQLImpl::customMessageProcess(std::shared_ptr<InfoLoggerMe
350350
if (commitEnabled) {
351351
if (commitNumberOfMsg == 0) {
352352
if (mysql_query(db, "START TRANSACTION")) {
353-
theLog->error("DB start transaction failed: %s", mysql_error(db));
353+
parent->logError("DB start transaction failed: %s", mysql_error(db));
354354
commitEnabled = 0;
355355
return returnDelayedMessage();
356356
} else {
357357
if (commitDebug) {
358-
theLog->info("DB transaction started");
358+
parent->logInfo("DB transaction started");
359359
}
360360
}
361361
commitTimer.reset(commitTimeout);
@@ -375,15 +375,15 @@ int InfoLoggerDispatchSQLImpl::customMessageProcess(std::shared_ptr<InfoLoggerMe
375375

376376
// update bind variables
377377
if (mysql_stmt_bind_param(stmt, bind)) {
378-
theLog->error("mysql_stmt_bind() failed: %s", mysql_error(db));
379-
theLog->error("message: %s", msg);
378+
parent->logError("mysql_stmt_bind() failed: %s", mysql_error(db));
379+
parent->logError("message: %s", msg);
380380
// if can not bind, message malformed, drop it
381381
return returnDroppedMessage(msg, m);
382382
}
383383

384384
// Do the insertion
385385
if (mysql_stmt_execute(stmt)) {
386-
theLog->error("mysql_stmt_exec() failed: (%d) %s", mysql_errno(db), mysql_error(db));
386+
parent->logError("mysql_stmt_exec() failed: (%d) %s", mysql_errno(db), mysql_error(db));
387387
// column too long
388388
if (mysql_errno(db) == ER_DATA_TOO_LONG) {
389389
return returnDroppedMessage(msg, m);
@@ -398,7 +398,7 @@ int InfoLoggerDispatchSQLImpl::customMessageProcess(std::shared_ptr<InfoLoggerMe
398398

399399
if (commitDebug) {
400400
if (insertCount % 1000 == 0) {
401-
theLog->info("insert count = %llu", insertCount);
401+
parent->logInfo("insert count = %llu", insertCount);
402402
}
403403
}
404404
}

0 commit comments

Comments
 (0)