2

I use cpp under Linux and I want to use the log4cpp.

I have tried to use it under windows with vs2013 and it worked very well. Now I am working under Linux and I got a problem:
It doesn't work with file. Here is my test code:

int main(int argc, char* argv[])
{
    fstream logFile;
    logFile.open("log", std::ios::app);
    log4cpp::OstreamAppender* osAppender = new log4cpp::OstreamAppender("osAppender", &logFile);
    // I tried cout as below and it worked, but if I tried as above with a file, it didn't work anymore.
    // I mean the "log" file could be created but the message can't be written down in the file. The file is always empty.
    //log4cpp::OstreamAppender* osAppender = new log4cpp::OstreamAppender("osAppender", &cout);

    osAppender->setLayout(new log4cpp::BasicLayout());

    log4cpp::Category& root = log4cpp::Category::getRoot();
    root.addAppender(osAppender);
    root.setPriority(log4cpp::Priority::DEBUG);
    root.error("Hello log4cpp in aError Message!");
    root.warn("Hello log4cpp in aWarning Message!");
    log4cpp::Category::shutdown();

    cout<<"test";

    return 0;
}

I ran this test code many times, I got no error and the program finished successfully because I can see "test" at the console. But the file is always empty.

By the way, sudo chmod +777 log has been done. So it couldn't be the problem of permission.

Yves
  • 11,597
  • 17
  • 83
  • 180
  • You're missing a bit of description. What happens? Do you get any log4cxx errors at the console? – mskfisher Apr 24 '15 at 13:01
  • "It doesn't work with file" isn't very useful. You need to provide more detailed information. – Andrew Henle Apr 24 '15 at 13:02
  • @mskfisher I added more description. – Yves Apr 24 '15 at 13:23
  • @AndrewHenle I added more description. – Yves Apr 24 '15 at 13:23
  • 1
    How do you know if `logFile.open("log", std::ios::app);` worked or not? – Andrew Henle Apr 24 '15 at 13:27
  • @AndrewHenle before the project, there is no file named "log", after running the project, an empty file named "log" is creted. So I think it means logFile.open() did work. – Yves Apr 24 '15 at 13:33
  • Run your program under strace: `strace -f -o strace.out ./yourProgramName`. There should be a line in `strace.out` similar to `open( "log", O_RDWR | O_APPEND, 0644) = 4`. That's the `open()` syscall that actually opens the file. I'm wondering if the flags are getting passed wrongly, of if something is happening to the contents afterwards. The `strace` output would show that. – Andrew Henle Apr 24 '15 at 13:57
  • @AndrewHenle I'm sorry i dont quite know about gcc. I am using Netbeans. In my project, I didn't find the executable file. There are all of my head files and cpp files, a file named Makefile and a folder named build, where I find the obj files. But I didn't file the executable file. I think the "yourProgramName" is an executable file right? – Yves Apr 24 '15 at 14:20
  • @Thomas - The program is probably under something like `dist/linux-x86_64/debug` in your Netbeans project directory. – Andrew Henle Apr 24 '15 at 14:23
  • @AndrewHenle I tried this: `gcc -o main main.cpp` but I got this error: `undefined reference: std::cout` omg...... It's weird. I compiled it with Netbeans and it worked. Now I compile it with the command line and it doesn't even know `std::cout` ?? – Yves Apr 24 '15 at 14:24
  • Use `g++` instead of `gcc`. `gcc` is a C compiler, not a C++ compiler. – Andrew Henle Apr 24 '15 at 14:33
  • @AndrewHenle OK I found the executable file. And I also run `strace -f -o strace.out ./yourProgramName`. Now I got a file named strace.out. I try to `cat strace.out | grep log` and I got this: `open("log", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3` – Yves Apr 24 '15 at 14:34
  • Do you seen any `write`(3,...)` lines in the file? – Andrew Henle Apr 24 '15 at 14:53
  • @AndrewHenle no, I got only this: `write(1, "aaaaaa", 6) = 6` – Yves Apr 24 '15 at 14:56
  • @AndrewHenle I ve tried this: `log4cpp::OstreamAppender* osAppender = new log4cpp::OstreamAppender("osAppender", &cout);`. In this case, I could get three `write` in the file strace.out. So still, the message could be written at the console but didn't work for the file. – Yves Apr 24 '15 at 15:00
  • @AndrewHenle I know why I got this problem now.... the problem came from here: `Ios::app`. I changed it into `ios::out` and it works now......well, i dont know what to say now...`ios::app` did work under windows... – Yves Apr 24 '15 at 15:31
  • @AndrewHenle but `ios::out` isn't what I need. Because it always erase the old messages.... Do you know how to add messages into a file? – Yves Apr 24 '15 at 15:34
  • You want both, I'd think. If you don't have `ios::app` you'll overwrite the file each time you restart. – Andrew Henle Apr 24 '15 at 15:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76193/discussion-between-thomas-and-andrew-henle). – Yves Apr 24 '15 at 15:49
  • @AndrewHenle look at my answer. I got it at last~~ :D thank you a lot. I learnt a lot from you. – Yves Apr 24 '15 at 15:55

1 Answers1

2

The problem is here:

 fstream logFile;
 logFile.open("log", std::ios::app);

What I need is:
If there is no file, create it and write message in it;
If there has been the file, apprend the message in it.

To achieve this goal, we have two ways:

 ofstream logFile;
 logFile.open("log", std::ios::app);

OR

 fstream logFile;
 logFile.open("log", std::ios::app | std::ios::out);
Yves
  • 11,597
  • 17
  • 83
  • 180