wook815 / google-glog

Automatically exported from code.google.com/p/google-glog
Other
0 stars 0 forks source link

how glog used in cgi? #54

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
If use glog in cgi, each time when cgi is run, there would be a new log file. 
How can i use glog in this condition to get the same log file each time?

Original issue reported on code.google.com by owandyw...@gmail.com on 1 Jul 2010 at 8:26

GoogleCodeExporter commented 8 years ago
We don't support the feature. You may need to remove log files periodically.

Original comment by shinichi...@gmail.com on 1 Jul 2010 at 2:18

GoogleCodeExporter commented 8 years ago
For the record, glog does reopen the same log file in append mode. The problem 
is that the date-time-pid string will be different for each execution of the 
cgi process.

If you patch the portion of logging.cc that creates the file (I believe it is 
in Write() ) to not add that portion... it may work. Though there may be some 
race conditions if the cgi process gets started multiple times at once.

But I am no one important ... as shinichiro.hamaji (who is someone important) 
says, it is not officially supported.

Original comment by donald.b.guy on 1 Jul 2010 at 5:54

GoogleCodeExporter commented 8 years ago
FYI, if you are happy to write file management works by your self, you may be 
able to use SetLogger like following code.

#include <stdio.h>
#include <sys/file.h>

#include <string>

#include <glog/logging.h>

using namespace google;
using namespace std;

struct MyLogger : public base::Logger {
  virtual void Write(bool should_flush,
                     time_t timestamp,
                     const char* message,
                     int length) {
    FILE* fp = fopen("/tmp/out", "a");
    flock(fileno(fp), LOCK_EX);
    fprintf(fp, "%.*s", length, message);
    fclose(fp);
  }

  virtual void Flush() { }

  virtual uint32 LogSize() { return 0; }
};

struct NullLogger : public base::Logger {
  virtual void Write(bool should_flush,
                     time_t timestamp,
                     const char* message,
                     int length) {
  }

  virtual void Flush() { }

  virtual uint32 LogSize() { return 0; }
};

void SetMyLogger(int severity, base::Logger* logger) {
  base::Logger* old_logger = base::GetLogger(severity);
  base::SetLogger(severity, logger);
  FlushLogFiles(severity);
}

int main(int argc, char* argv[]) {
  InitGoogleLogging(argv[0]);

  MyLogger my_logger;
  NullLogger null_logger;
  SetMyLogger(INFO, &my_logger);
  SetMyLogger(WARNING, &null_logger);
  SetMyLogger(ERROR, &null_logger);
  SetMyLogger(FATAL, &null_logger);

  LOG(INFO) << "info";
  LOG(WARNING) << "warn";
  LOG(ERROR) << "error";
  LOG(FATAL) << "fatal";
}

Original comment by shinichi...@gmail.com on 1 Jul 2010 at 6:38