Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00045
00046
00047
00048 class MetaInfo
00049 {
00050 public:
00051 enum
00052 {
00053 DATA_FROM_METAFILE = 1,
00054
00055 VALID_CREATION_TIME = 2,
00056 VALID_SIGNATURE = 4,
00057
00058 FILE_OPEN_SUCCESSFUL = 8
00059 };
00060
00061 enum
00062 {
00063 BUF_SIZE = 640
00064 };
00065
00066 private:
00067 char *_filename;
00068 time_t _creation_time;
00069 uint64_t _log_object_signature;
00070 int _flags;
00071 char _buffer[BUF_SIZE];
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
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; };
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;
00183 MetaInfo *m_meta_info;
00184
00185 size_t m_ascii_buffer_size;
00186 size_t m_max_line_size;
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;
00193
00194 public:
00195 Link<LogFile> link;
00196
00197 private:
00198
00199 LogFile();
00200 LogFile & operator=(const LogFile &);
00201 };
00202
00203
00204
00205
00206
00207 #endif