write log information into a file (performed)
-
a day ago i had a doubt about how to save log information into a file... searching on qt forum i already found answers....
first found this... code but the problem is that idk where is saved the file.... 'context.file'
const char*const applicationName="myapp"; #ifdef ANDROIDQUIRKS // Set in my myapp.pro file for android builds #include <android/log.h> void myMessageHandler( QtMsgType type, const QMessageLogContext& context, const QString& msg ) { QString report=msg; if (context.file && !QString(context.file).isEmpty()) { report+=" in file "; report+=QString(context.file); report+=" line "; report+=QString::number(context.line); } if (context.function && !QString(context.function).isEmpty()) { report+=+" function "; report+=QString(context.function); } const char*const local=report.toLocal8Bit().constData(); switch (type) { case QtDebugMsg: __android_log_write(ANDROID_LOG_DEBUG,applicationName,local); break; case QtInfoMsg: __android_log_write(ANDROID_LOG_INFO,applicationName,local); break; case QtWarningMsg: __android_log_write(ANDROID_LOG_WARN,applicationName,local); break; case QtCriticalMsg: __android_log_write(ANDROID_LOG_ERROR,applicationName,local); break; case QtFatalMsg: default: __android_log_write(ANDROID_LOG_FATAL,applicationName,local); abort(); } } #endif ... int main(int argc,char* argv[]) { QGuiApplication app(argc,argv); #ifdef ANDROIDQUIRKS qInstallMessageHandler(myMessageHandler); #endif app.setApplicationName(applicationName); ..
later i found this solution that save file on Documents folders.... but sometimes this crashes and maybe because open and close the file everytime...
if anyone know how to solve both problems could be nice for me....void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { QString folder=QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); static QFile logFile(folder+"/BUFFER_PPT.txt"); static QTextStream ts(&logFile); if(logFile.open(QFile::ReadWrite | QFile::Append)) { ts << context.file << ":" << context.line << ":" << context.function << ": " << msg << endl; logFile.close(); } }
-
a day ago i had a doubt about how to save log information into a file... searching on qt forum i already found answers....
first found this... code but the problem is that idk where is saved the file.... 'context.file'
const char*const applicationName="myapp"; #ifdef ANDROIDQUIRKS // Set in my myapp.pro file for android builds #include <android/log.h> void myMessageHandler( QtMsgType type, const QMessageLogContext& context, const QString& msg ) { QString report=msg; if (context.file && !QString(context.file).isEmpty()) { report+=" in file "; report+=QString(context.file); report+=" line "; report+=QString::number(context.line); } if (context.function && !QString(context.function).isEmpty()) { report+=+" function "; report+=QString(context.function); } const char*const local=report.toLocal8Bit().constData(); switch (type) { case QtDebugMsg: __android_log_write(ANDROID_LOG_DEBUG,applicationName,local); break; case QtInfoMsg: __android_log_write(ANDROID_LOG_INFO,applicationName,local); break; case QtWarningMsg: __android_log_write(ANDROID_LOG_WARN,applicationName,local); break; case QtCriticalMsg: __android_log_write(ANDROID_LOG_ERROR,applicationName,local); break; case QtFatalMsg: default: __android_log_write(ANDROID_LOG_FATAL,applicationName,local); abort(); } } #endif ... int main(int argc,char* argv[]) { QGuiApplication app(argc,argv); #ifdef ANDROIDQUIRKS qInstallMessageHandler(myMessageHandler); #endif app.setApplicationName(applicationName); ..
later i found this solution that save file on Documents folders.... but sometimes this crashes and maybe because open and close the file everytime...
if anyone know how to solve both problems could be nice for me....void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { QString folder=QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); static QFile logFile(folder+"/BUFFER_PPT.txt"); static QTextStream ts(&logFile); if(logFile.open(QFile::ReadWrite | QFile::Append)) { ts << context.file << ":" << context.line << ":" << context.function << ": " << msg << endl; logFile.close(); } }
-
@piervalli i found that the reason of crashes is that in other thread im trying to "clean" the file but... removing file from device, do you know a way to clean a text file without deleting file?
-
@piervalli i found that the reason of crashes is that in other thread im trying to "clean" the file but... removing file from device, do you know a way to clean a text file without deleting file?
-
@0xNull Try to add a QMutex with lock and unclock because if the application is multi thread you can can a conflit. I use the same approce withouth crash.
@piervalli said in write log information into a file (performed):
QMutex
it could fix this error
F libc : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x450042004d0041 in tid 21304 (QThread), pid 21119
-
@piervalli said in write log information into a file (performed):
QMutex
it could fix this error
F libc : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x450042004d0041 in tid 21304 (QThread), pid 21119
-
@piervalli said in write log information into a file (performed):
QMutex
it could fix this error
F libc : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x450042004d0041 in tid 21304 (QThread), pid 21119
@mpergand 041 in tid 21304 (QThread), pid 21119 is id of main thread? You can rename/delete in startup of the app.
This is nice link on Logger https://www.youtube.com/watch?v=GDtrbIs6JA0I have the same approce on my app, I rename/delete file on start appup of the function. I don't know why logfile is not closed. but it works.
-
@mpergand 041 in tid 21304 (QThread), pid 21119 is id of main thread? You can rename/delete in startup of the app.
This is nice link on Logger https://www.youtube.com/watch?v=GDtrbIs6JA0I have the same approce on my app, I rename/delete file on start appup of the function. I don't know why logfile is not closed. but it works.
@piervalli where is saved logfile.txt?
-
@piervalli where is saved logfile.txt?
@0xNull said in write log information into a file (performed):
where is saved logfile.txt?
Since a relative path is used it will be in current working directory.
-
@piervalli where is saved logfile.txt?
@0xNull
As @jsulm says. And since you do not know what the current directory will be at runtime, and it might not even be writable by your application, do not use such a relative path in production. You might have a look at QStandardPaths Class and pick a suitableQStandardPaths::StandardLocation
to use to build an absolute path for your log file location. -
@piervalli where is saved logfile.txt?