@@ -28,8 +28,8 @@ typedef bool my_bool;
2828class 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
124124void 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
130130InfoLoggerDispatchSQL::~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