00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 #include <libplumbing/config.h>
00019 #include <cerrno>
00020 #include <cstring>
00021 #include <libexplain/fopen.h>
00022 #include <libexplain/fflush.h>
00023 
00024 #include <libplumbing/logging/file.h>
00025 #include <libplumbing/progname.h>
00026 
00027 
00028 plumbing::logging_file::~logging_file()
00029 {
00030     if (vfp)
00031     {
00032         FILE *fp = (FILE *)vfp;
00033         vfp = 0;
00034         fclose(fp);
00035     }
00036 }
00037 
00038 
00039 plumbing::logging_file::logging_file(const std::string &a_filename) :
00040     logging(),
00041     filename(a_filename),
00042     vfp(0),
00043     since(0)
00044 {
00045 }
00046 
00047 
00048 void
00049 plumbing::logging_file::deliver_message_text(const char *txt)
00050 {
00051     if (vfp)
00052     {
00053         time_t now;
00054         time(&now);
00055         if (now - since >= 60 * 60)
00056         {
00057             
00058             
00059             
00060             
00061             FILE *fp = (FILE *)vfp;
00062             vfp = 0;
00063             fclose(fp);
00064         }
00065     }
00066 
00067     FILE *fp = (FILE *)vfp;
00068     if (!fp)
00069     {
00070         fp = fopen(filename.c_str(), "a");
00071         if (!fp)
00072         {
00073             int err = errno;
00074 
00075             
00076             
00077             
00078             
00079             fprintf
00080             (
00081                 stderr,
00082                 "%s: %s\n",
00083                 progname_get().c_str(),
00084                 explain_errno_fopen(err, filename.c_str(), "a")
00085             );
00086             fp = stderr;
00087         }
00088         vfp = fp;
00089     }
00090 
00091     fprintf(fp, "%s\n", txt);
00092     if (fflush(fp))
00093     {
00094         int err = errno;
00095         fprintf
00096         (
00097             stderr,
00098             "%s: %s\n",
00099             progname_get().c_str(),
00100             explain_errno_fflush(err, fp)
00101         );
00102         fclose(fp);
00103         vfp = 0;
00104     }
00105 }
00106 
00107 
00108