no return vale :o - Linux

This is a discussion on no return vale :o - Linux ; Hi, I'm looking to figure out, why the ofstream: pen() method wouldn't work properly: I have this code: [C++] ofstream LogFile("/var/log/PRGmessages", ios: ut|ios::app); // opening the log file in appending mode if (!LogFile){ write("Could not open local log file for ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: no return vale :o

  1. no return vale :o

    Hi,

    I'm looking to figure out, why the ofstream:pen() method wouldn't work
    properly:
    I have this code:
    [C++]
    ofstream LogFile("/var/log/PRGmessages", ios:ut|ios::app); // opening the
    log file in appending mode
    if (!LogFile){
    write("Could not open local log file for writing", LOG_ERR);
    }
    LogFile << i_strData;
    LogFile.close();
    [/C++]
    I let it run over night and saw in the morning that there were plenty
    of "Could not open local log file for writing" messages. open() has no
    return value, so does anyone know how i could figure out what had gone
    wrong?
    Thanks,
    Ron
    --
    weeks of software enineering safe hours of planing

  2. Re: no return vale :o

    Ron Eggler wrote:

    > Hi,
    >
    > I'm looking to figure out, why the ofstream:pen() method wouldn't work
    > properly:
    > I have this code:
    > [C++]
    > ofstream LogFile("/var/log/PRGmessages", ios:ut|ios::app); // opening the
    > log file in appending mode
    > if (!LogFile){
    > write("Could not open local log file for writing", LOG_ERR);
    > }
    > LogFile << i_strData;
    > LogFile.close();
    > [/C++]
    > I let it run over night and saw in the morning that there were plenty
    > of "Could not open local log file for writing" messages. open() has no
    > return value, so does anyone know how i could figure out what had gone
    > wrong?


    AFAIK, with standard C++ code you can't, you have to resort to other
    functions, such as access() or similar. Anyway, your code is conceptually
    broken, it should be something like that:


    // opening the log file in appending mode
    ofstream LogFile("/var/log/PRGmessages", ios:ut|ios::app);
    if( LogFile ){
    LogFile << i_strData;
    LogFile.close();
    }
    else {
    write("Could not open local log file for writing", LOG_ERR);
    }


    --
    SF

    Games are very educational. Scrabble teaches us vocabulary, Monopoly teaches
    us cash-flow management, and Dungeons & Dragons teaches us to loot dead bodies.

  3. Re: no return vale :o

    Dildo Bogumil di Boscopelo wrote:

    > Ron Eggler wrote:
    >
    >> Hi,
    >>
    >> I'm looking to figure out, why the ofstream:pen() method wouldn't work
    >> properly:
    >> I have this code:
    >> [C++]
    >> ofstream LogFile("/var/log/PRGmessages", ios:ut|ios::app);
    >> // opening the
    >> log file in appending mode
    >> if (!LogFile){
    >> write("Could not open local log file for writing",
    >> LOG_ERR);
    >> }
    >> LogFile << i_strData;
    >> LogFile.close();
    >> [/C++]
    >> I let it run over night and saw in the morning that there were plenty
    >> of "Could not open local log file for writing" messages. open() has no
    >> return value, so does anyone know how i could figure out what had gone
    >> wrong?

    >
    > AFAIK, with standard C++ code you can't, you have to resort to other
    > functions, such as access() or similar. Anyway, your code is conceptually
    > broken, it should be something like that:
    >
    >
    > // opening the log file in appending mode
    > ofstream LogFile("/var/log/PRGmessages", ios:ut|ios::app);
    > if( LogFile ){
    > LogFile << i_strData;
    > LogFile.close();
    > }
    > else {
    > write("Could not open local log file for writing", LOG_ERR);
    > }
    >


    I did realize that my code isn't correct and i came up with this solution:

    static bool LogErr=0;
    ofstream LogFile("/var/log/PRGmessages", ios:ut|ios::app); // opening the
    log file in appending mode

    if (!LogFile && !LogErr){ // only log the error if it hasn't logged it
    before (unless it could cause a stack overflow...
    write("Could not open local log file for writing", LOG_ERR);//... since
    write() calls this current method and it would be hung up in here).
    LogErr=1;
    }
    else if(LogFile) {
    LogErr=0;
    LogFile << i_strData;
    LogFile.close();
    }

    Is this any good? Well it works so far

    >


    --
    weeks of software enineering safe hours of planing

  4. Re: no return vale :o

    Ron Eggler wrote:
    > I did realize that my code isn't correct and i came up with this solution:
    >
    > static bool LogErr=0;
    > ofstream LogFile("/var/log/PRGmessages", ios:ut|ios::app);
    > // opening the log file in appending mode
    >
    > if (!LogFile && !LogErr){
    > // only log the error if it hasn't logged it
    > // before (unless it could cause a stack overflow...
    > write("Could not open local log file for writing",
    > LOG_ERR);
    > // ... since write() calls this current method and
    > // it would be hung up in here).
    > LogErr=1;
    > }
    > else if(LogFile) {
    > LogErr=0;
    > LogFile << i_strData;
    > LogFile.close();
    > }
    >

    > Is this any good? Well it works so far


    A few things I would mention:
    - Using a function-static bool would make the code non thread-safe.
    - Use 'true' and 'false' instead of '1' and '0'.
    - The 'o' in ofstream means that it is for output. That means that you can
    drop the ios:ut argument, it is implicitly always there. You only need
    that for a plain fstream.
    - No need to close() the stream, that's what the destructor does.
    - If you want to play safe, invoke flush() after writing to see if the data
    arrived.
    - I get that this is from an application and that it's part of logging.
    What I would do is open the file once on startup and only write to it later
    on. Further, I would generate an error message on startup or even terminate
    if the file can't be opened. Lateron, I could even imagine ignoring whether
    the logging is operational, i.e. I would not generate additional log
    messages from failed logging attempts.


    Uli


+ Reply to Thread