MrmDdda
MrmDdda

Reputation: 51

Real-time write to file, C

I'm trying to build a file in which I will keep the most important events from the application. I built a function responsible for logging but it does not seem to work as expected. First of all messages do not appear in APPLOG.LOG file during infinite while() loop, but only when application is finished. How should I build the function to write information to the file in real time? I use a lot of *.c files and in each of them I would like to be able to log events. Shouldn'tfLog = fopen(LOG_FILENAME, "w"); be inside the APLOG(...) function? Below is some code responsible for logging.

logging.h

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>

#define TYPE_GENERAL                1
#define TYPE_BINARY                 2
#define TYPE_INFO                   3
#define TYPE_WARNING                4
#define TYPE_ERROR                  5

#define LOG_FILENAME                "APPLOG.LOG"

FILE *fLog;

logging.c

void APLOG(FILE *fLog, uint32_t type, char *MESSAGE)
{    
    if(fLog == NULL)
    {
        printf("Error open %s FILE!", TRLOG_FILENAME);   
        exit(1);             
    }
    else
    {
        switch(type)
        {
            case TYPE_GENERAL:
                fprintf(fLog, "%s %s %s: %s \n",__DATE__, __TIME__, "GENERAL", MESSAGE);
                break;
            case TYPE_BINARY:
                fprintf(fLog, "%s %s %s: %s \n",__DATE__, __TIME__, "BINARY", "ATTEMPTED TO WRITE A BINARY MESSAGE. FUNCTION NOT ACTIVE");
                break;
            case TYPE_INFO:
                fprintf(fLog, "%s %s %s: %s \n",__DATE__, __TIME__, "INFO", MESSAGE);
                break;
            case TYPE_WARNING:
                fprintf(fLog, "%s %s %s: %s \n",__DATE__, __TIME__, "WARINNG", MESSAGE);
                //send warning to external app
                break;
            case TYPE_ERROR:
                fprintf(fLog, "%s %s %s: %s \n",__DATE__, __TIME__, "ERROR", MESSAGE);
                //send raport to app provider
                break;
            default:
                break;
        }
    }
}

main.c

int main(int argc, char **argv)
{
    fLog = fopen(LOG_FILENAME, "w");
    
    APLOG(fLog, TYPE_INFO, "APP HAS BEEN RUNNING. LOGGING STARTED" );

    while()
    {
        //endless while here. If any part of the code does not work, exit the while and report the problem. More APLOG here.
    }

    APLOG(fLog, TYPE_ERROR, "OUT OF WORK. BACK TO EMERGENCY."); 
    return 0;   
}

Upvotes: 1

Views: 395

Answers (1)

Jean-Loup Sabatier
Jean-Loup Sabatier

Reputation: 779

FILE * is a stream, which means writing to the file is bufferized and the data is actually written when it's the most convenient for the system (to maximize performance).

You could do it a bit more directly with the set of functions that are not using streams: open/close/write/read/... but even with these functions, the writing will be buffered.

With "streamed" access to files (fopen/fprintf/fclose/...), there is a way to actually write on disk all the data that is waiting to be written.. This is the function fflush

If you want your application to actually flush its buffer all the time you must use fflush(fLog); at the end of APLOG so every fprintf will be followed by a fflush...

Upvotes: 0

Related Questions