Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qDebug Console output with Timestamp

I wrote a qt small console utility for testing database connectivity, the code block is:

db.setHostName("hostIP");    
db.setDatabaseName("name");    
db.setUserName("uid");    
db.setPassword("pw");    
db.setPort(1521);

while(true)
{
    if (db.open())
    {
        qDebug()<<"OPEN";
        db.close();
    }
    else
    {
        qDebug()<<"YOU MESSED UP "<<db.lastError().text();
    }
}

the console output when the database is down or cannot be connected can be seen in the console output, what I want is to tee the timestamp also.

Is there a way to print the event timestamp on the console along with the qDebug messages ???

like image 970
RicoRicochet Avatar asked Dec 04 '14 09:12

RicoRicochet


3 Answers

You could install a message handler and print out the timestamp before every message. See the documentation of qInstallMessageHandler for this. With this solution you don't have to add QTimer::currentTime() on every call of qDebug().

like image 199
tomvodi Avatar answered Sep 22 '22 13:09

tomvodi


Sure you can. For doing so you can use QTime class, i.e.:

qDebug() << QTime::currentTime().toString() << "YOU MESSED UP "<< db.lastError().text();

Or, in the same way you can print out the date and time information with using QDateTime::currentDateTime() function.

like image 26
vahancho Avatar answered Sep 20 '22 13:09

vahancho


If you are on linux, you can set QT_MESSAGE_PATTERN environment variable,as explained here:

QT_MESSAGE_PATTERN="[%{type}] %{appname} (%{file}:%{line}) - %{message}"

like image 41
BЈовић Avatar answered Sep 24 '22 13:09

BЈовић