i create a project under QTCreater. i want to save all the errors in a log file and send it to the program support to solve these errors.
wha开发者_StackOverflowt i mean is to auto generate the file and auto send it when error appear. i have more than 20 source file i don't like in each method to call a function to insert data in the file and save it. what's i need is to do as filters in grails applications
is there a way to do that?
thx for you help
i use this scenario try it may help you:
1- create a form that has 2 buttons one to start recording and other to stop. (record form)
2- declare a pointer of type record form in the header, and create a new instance in th constructor.
3- in the record form there is 2 methods and one variable:
in header:
private
QString fileName;
in constructor:
conf = new conf(this);
connect(ui->btnStartRecording,SIGNAL(clicked()),this,SLOT(on_btnStartRecording_clicked()));
connect(ui->btnStopRecording,SIGNAL(clicked()),this,SLOT(on_btnStopRecording_clicked()));
first method:
void record::on_btnStartRecording_clicked(){
fileName = QFileDialog::getSaveFileName(this, tr("Save As"), ".log");
if (!fileName.isEmpty()){
if (!fileName.contains(".log")){
fileName = fileName + ".log";
}
conf->open_close_file(fileName,true);
QMessageBox::warning(this, "Start Recording", "Start Recording");
ui->btnStartRecording->setDisabled(true);
ui->btnStopRecording->setDisabled(false);
}
}
second method:
void record::on_btnStopRecording_clicked(){
if (!fileName.isEmpty()){
conf->open_close_file(fileName,false);
QMessageBox::warning(this, "Stop Recording", "Stop Recording");
ui->btnStartRecording->setDisabled(false);
ui->btnStopRecording->setDisabled(true);
}
}
4- conf is a class that has another 2 methods and 1 variable:
variable:
QFile *file;
first method:
void conf::open_close_file(QString fileName, bool boolFlag){
if (boolFlag == true){
file->setFileName(fileName);
if(file->open(QFile::WriteOnly | QFile::Text)){
}
}else{
file->close();
file->setFileName("");
}
}
second method:
void conf::write_in_file(QString data){
if (flag == true){
QDateTime time;
QString dataEntry = ">>> " + time.currentDateTime().toString("dd/MM/yyyy [hh:mm:ss.zzz]") + ": " + data;
QByteArray byte;
byte.append(dataEntry);
QDataStream out(file);
out<<byte<<"\n";
}
}
5- call write_in_file method in each method you want to save any error occurs in it.
6- finally click on stop record and open the file to see the result
Note:
a- boolflag used to differ a method call for open or close the file.
b- you can close the record form after you click on the start button and work on your program to have an error then open it another time and click stop (you must use this.accept and this.reject)
hope this may help you
catch all the errors via the normal try/catch (I prefer if/else checking) and send the message to a self developed Logging class which will output this to a file?
QTextStream
精彩评论