1 /****************************************************************************
2  *
3  * Copyright (C) 2014-2021 Cisco and/or its affiliates. All rights reserved.
4  * Copyright (C) 2003-2013 Sourcefire, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License Version 2 as
8  * published by the Free Software Foundation.  You may not use, modify or
9  * distribute this program under any other version of the GNU General
10  * Public License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  *
21  ****************************************************************************/
22 
23 /**
24  * @file   sf_textlog.h
25  * @author Russ Combs <rcombs@sourcefire.com>
26  * @date   Fri Jun 27 10:34:37 2003
27  *
28  * @brief  declares buffered text stream for logging
29  *
30  * Declares a TextLog_*() api for buffered logging.  This allows
31  * relatively painless transition from fprintf(), fwrite(), etc.
32  * to a buffer that is formatted in memory and written with one
33  * fwrite().
34  *
35  * Additionally, the file is capped at a maximum size.  Beyond
36  * that, the file is closed, renamed, and reopened.  The current
37  * file always has the same name.  Old files are renamed to that
38  * name plus a timestamp.
39  */
40 
41 #ifndef _SF_TEXT_LOG_H
42 #define _SF_TEXT_LOG_H
43 
44 #include <stdio.h>
45 #include <string.h>
46 #include <time.h>
47 
48 #define K_BYTES (1024)
49 #define M_BYTES (K_BYTES*K_BYTES)
50 #define G_BYTES (K_BYTES*M_BYTES)
51 
52 /*
53  * DO NOT ACCESS STRUCT MEMBERS DIRECTLY
54  * EXCEPT FROM WITHIN THE IMPLEMENTATION!
55  */
56 typedef struct _TextLog
57 {
58 /* private: */
59 /* file attributes: */
60     FILE* file;
61     char* name;
62     size_t size;
63     size_t maxFile;
64     time_t last;
65 
66 /* buffer attributes: */
67     unsigned int pos;
68     unsigned int maxBuf;
69     char buf[1];
70 } TextLog;
71 
72 TextLog* TextLog_Init (
73     const char* name, unsigned int maxBuf, size_t maxFile
74 );
75 void TextLog_Term (TextLog* this);
76 
77 bool TextLog_Putc(TextLog*, char);
78 bool TextLog_Quote(TextLog*, const char*);
79 bool TextLog_Write(TextLog*, const char*, int len);
80 bool TextLog_Print(TextLog*, const char* format, ...);
81 bool TextLog_PrintUnicode(TextLog*, uint8_t *, uint32_t, uint8_t);
82 
83 bool TextLog_Flush(TextLog*);
84 
85 /*-------------------------------------------------------------------
86   * helper functions
87   *-------------------------------------------------------------------
88   */
TextLog_Tell(TextLog * this)89  static inline int TextLog_Tell (TextLog* this)
90  {
91      return this->pos;
92  }
93 
TextLog_Avail(TextLog * this)94  static inline int TextLog_Avail (TextLog* this)
95  {
96      return this->maxBuf - this->pos - 1;
97  }
98 
TextLog_Reset(TextLog * this)99  static inline void TextLog_Reset (TextLog* this)
100  {
101      this->pos = 0;
102      this->buf[this->pos] = '\0';
103  }
104 
TextLog_NewLine(TextLog * this)105 static inline bool TextLog_NewLine (TextLog* this)
106 {
107     return TextLog_Putc(this, '\n');
108 }
109 
TextLog_Puts(TextLog * this,const char * str)110 static inline bool TextLog_Puts (TextLog* this, const char* str)
111 {
112     return TextLog_Write(this, str, strlen(str));
113 }
114 
115 #endif /* _SF_TEXT_LOG_H */
116 
117