"Fossies" - the Fresh Open Source Software Archive

Member "postal-0.76/logit.cpp" (26 Jan 2021, 1364 Bytes) of package /linux/privat/postal-0.76.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "logit.cpp" see the Fossies "Dox" file reference documentation.

    1 #include "logit.h"
    2 #include <limits.h>
    3 #include <cstring>
    4 
    5 Logit::Logit(const char *filename, bool is_verbose, bool numbered_files, int pid)
    6  : m_verbose(is_verbose)
    7  , m_fp(NULL)
    8  , m_count(0)
    9  , m_numbered_files(numbered_files)
   10  , m_filename(filename)
   11  , m_pid(pid)
   12 {
   13 }
   14 
   15 Logit::Logit(const Logit &l, int pid)
   16  : m_verbose(l.m_verbose)
   17  , m_fp(NULL)
   18  , m_count(0)
   19  , m_numbered_files(l.m_numbered_files)
   20  , m_filename(l.m_filename)
   21  , m_pid(pid)
   22 {
   23 }
   24 
   25 // for HURD
   26 #ifndef PATH_MAX
   27 #define PATH_MAX 4096
   28 #endif
   29 
   30 bool Logit::reopen()
   31 {
   32   if(!m_numbered_files && m_fp)
   33     return false;
   34   char buf[PATH_MAX];
   35 
   36   if(m_fp)
   37   {
   38     fflush(m_fp);
   39     fclose(m_fp);
   40   }
   41   char pid[12], count[12];
   42   sprintf(pid, "%d", m_pid);
   43   sprintf(count, "%d", m_count);
   44   strncpy(buf, m_filename.c_str(), PATH_MAX - 25);
   45   buf[PATH_MAX - 25] = '\0';
   46   if(m_pid)
   47   {
   48     strcat(buf, ":");
   49     strcat(buf, pid);
   50   }
   51   if(m_numbered_files)
   52   {
   53     strcat(buf, ":");
   54     strcat(buf, count);
   55   }
   56   m_count++;
   57   m_fp = fopen(buf, "w");
   58   if(!m_fp)
   59   {
   60     fprintf(stderr, "Can't open file %s\n", buf);
   61     m_fp = fdopen(2, "w");
   62   }
   63   return false;
   64 }
   65 
   66 Logit::~Logit()
   67 {
   68   if(m_fp)
   69   {
   70     fflush(m_fp);
   71     fclose(m_fp);
   72   }
   73 }
   74 
   75 int Logit::Write(const char *data, size_t len)
   76 {
   77   if(!m_fp)
   78     reopen();
   79   if(fwrite(data, 1, len, m_fp) != len)
   80     return -1;
   81   fflush(m_fp);
   82   return 0;
   83 }
   84