• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

LogFile.h

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 
00025 
00026 #ifndef LOG_FILE_H
00027 #define LOG_FILE_H
00028 
00029 #include <stdarg.h>
00030 #include <stdio.h>
00031 
00032 #include "libts.h"
00033 #include "LogBufferSink.h"
00034 
00035 class LogSock;
00036 class LogBuffer;
00037 struct LogBufferHeader;
00038 class LogObject;
00039 
00040 #define LOGFILE_ROLLED_EXTENSION ".old"
00041 #define LOGFILE_SEPARATOR_STRING "_"
00042 
00043 /*-------------------------------------------------------------------------
00044   MetaInfo
00045 
00046   Meta information for LogFile
00047   -------------------------------------------------------------------------*/
00048 class MetaInfo
00049 {
00050 public:
00051   enum
00052   {
00053     DATA_FROM_METAFILE = 1,     // metadata was read (or attempted to)
00054     // from metafile
00055     VALID_CREATION_TIME = 2,    // creation time is valid
00056     VALID_SIGNATURE = 4,        // signature is valid
00057     // (i.e., creation time only)
00058     FILE_OPEN_SUCCESSFUL = 8   // metafile was opened successfully
00059   };
00060 
00061   enum
00062   {
00063     BUF_SIZE = 640              // size of read/write buffer
00064   };
00065 
00066 private:
00067   char *_filename;              // the name of the meta file
00068   time_t _creation_time;        // file creation time
00069   uint64_t _log_object_signature; // log object signature
00070   int _flags;                   // metainfo status flags
00071   char _buffer[BUF_SIZE];       // read/write buffer
00072 
00073   void _read_from_file();
00074   void _write_to_file();
00075   void _build_name(const char *filename);
00076 
00077 public:
00078  MetaInfo(const char *filename)
00079    : _flags(0)
00080   {
00081     _build_name(filename);
00082     _read_from_file();
00083   }
00084 
00085   MetaInfo(char *filename, time_t creation, uint64_t signature)
00086     : _creation_time(creation), _log_object_signature(signature), _flags(VALID_CREATION_TIME | VALID_SIGNATURE)
00087   {
00088     _build_name(filename);
00089     _write_to_file();
00090   }
00091 
00092   ~MetaInfo()
00093   {
00094     ats_free(_filename);
00095   }
00096 
00097   bool get_creation_time(time_t * time)
00098   {
00099     if (_flags & VALID_CREATION_TIME) {
00100       *time = _creation_time;
00101       return true;
00102     } else {
00103       return false;
00104     }
00105   }
00106 
00107   bool get_log_object_signature(uint64_t * signature)
00108   {
00109     if (_flags & VALID_SIGNATURE) {
00110       *signature = _log_object_signature;
00111       return true;
00112     } else {
00113       return false;
00114     }
00115   }
00116 
00117   bool data_from_metafile() const { return (_flags & DATA_FROM_METAFILE ? true : false); }
00118   bool file_open_successful() { return (_flags & FILE_OPEN_SUCCESSFUL ? true : false); }
00119 };
00120 
00121 /*-------------------------------------------------------------------------
00122   LogFile
00123   -------------------------------------------------------------------------*/
00124 
00125 class LogFile:public LogBufferSink, public RefCountObj
00126 {
00127 public:
00128   LogFile(const char *name, const char *header, LogFileFormat format, uint64_t signature,
00129           size_t ascii_buffer_size = 4 * 9216, size_t max_line_size = 9216);
00130   LogFile(const LogFile &);
00131   ~LogFile();
00132 
00133   enum
00134   {
00135     LOG_FILE_NO_ERROR = 0,
00136     LOG_FILE_NO_PIPE_READERS,
00137     LOG_FILE_COULD_NOT_CREATE_PIPE,
00138     LOG_FILE_PIPE_MODE_NOT_SUPPORTED,
00139     LOG_FILE_COULD_NOT_OPEN_FILE,
00140     LOG_FILE_FILESYSTEM_CHECKS_FAILED
00141   };
00142 
00143   int preproc_and_try_delete(LogBuffer * lb);
00144 
00145   int roll(long interval_start, long interval_end);
00146 
00147   const char *get_name() const { return m_name; }
00148 
00149   void change_header(const char *header);
00150   void change_name(const char *new_name);
00151 
00152   LogFileFormat get_format() const { return m_file_format; }
00153   const char *get_format_name() const {
00154     return (m_file_format == LOG_FILE_BINARY ? "binary" : (m_file_format == LOG_FILE_PIPE ? "ascii_pipe" : "ascii"));
00155   }
00156 
00157   static int write_ascii_logbuffer(LogBufferHeader * buffer_header, int fd, const char *path, const char *alt_format = NULL);
00158   int write_ascii_logbuffer3(LogBufferHeader * buffer_header, const char *alt_format = NULL);
00159   static bool rolled_logfile(char *file);
00160   static bool exists(const char *pathname);
00161 
00162   void display(FILE * fd = stdout);
00163   int open_file();
00164 
00165   off_t get_size_bytes() const { return m_file_format != LOG_FILE_PIPE? m_bytes_written : 0; };
00166   int do_filesystem_checks() { return 0; }; // TODO: this need to be tidy up when to redo the file checking
00167 
00168 public:
00169   bool is_open() { return (m_fd >= 0); }
00170   void close_file();
00171 
00172   void check_fd();
00173   static int writeln(char *data, int len, int fd, const char *path);
00174   void read_metadata();
00175 
00176 public:
00177   LogFileFormat m_file_format;
00178 private:
00179   char *m_name;
00180 public:
00181   char *m_header;
00182   uint64_t m_signature;           // signature of log object stored
00183   MetaInfo *m_meta_info;
00184 
00185   size_t m_ascii_buffer_size;   // size of ascii buffer
00186   size_t m_max_line_size;       // size of longest log line (record)
00187 
00188   int m_fd;
00189   long m_start_time;
00190   long m_end_time;
00191   volatile uint64_t m_bytes_written;
00192   off_t m_size_bytes;           // current size of file in bytes
00193 
00194 public:
00195   Link<LogFile> link;
00196 
00197 private:
00198   // -- member functions not allowed --
00199   LogFile();
00200   LogFile & operator=(const LogFile &);
00201 };
00202 
00203 /***************************************************************************
00204  LogFileList IS NOT USED
00205 ****************************************************************************/
00206 
00207 #endif

Generated by  doxygen 1.7.1