Windows: AFSTearDownExtents may experience active extents
[openafs.git] / src / WINNT / afssvrcfg / logfile.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * INCLUDES ___________________________________________________________________
12  *
13  */
14 #include <winsock2.h>
15 #include <ws2tcpip.h>
16
17 extern "C" {
18 #include <afsconfig.h>
19 #include <afs/param.h>
20 #include <afs/stds.h>
21 #include <roken.h>
22 }
23
24 extern "C" {
25 #include <afs\afs_utilAdmin.h>
26 }
27
28 #include <windows.h>
29 #include <stdio.h>
30 #include "logfile.h"
31 #include <WINNT\talocale.h>
32 #include <time.h>
33
34
35 /*
36  * MEMBER FUNCTIONS _________________________________________________________
37  *
38  */
39 LOGFILE::LOGFILE()
40 {
41     m_fp = 0;
42 }
43
44 LOGFILE::~LOGFILE()
45 {
46     if (m_fp)
47         Close();
48 }
49
50 BOOL LOGFILE::Open(const char *pszLogFilePath,
51                    LOGFILE_OPEN_MODE eOpenMode,
52                    LOGFILE_TIMESTAMP_MODE eTimeStampMode)
53 {
54     char *pszOpenMode;
55
56     m_eTimeStampMode = eTimeStampMode;
57
58     if (eOpenMode == OM_OVERWRITE)
59         pszOpenMode = "w";
60     else
61         pszOpenMode = "a+";
62
63     strcpy(m_szPath, pszLogFilePath);
64
65     m_fp = fopen(pszLogFilePath, pszOpenMode);
66     if (m_fp) {
67         if (m_eTimeStampMode != TSM_NEVER)
68             WriteTimeStamp();
69         fprintf(m_fp, "Log file open.\r\n");
70
71         return TRUE;
72     }
73
74     return FALSE;
75 }
76
77 BOOL LOGFILE::Close()
78 {
79     int nResult = 0;
80
81     if (m_fp) {
82         if (m_eTimeStampMode != TSM_NEVER)
83             WriteTimeStamp();
84         fprintf(m_fp, "Closing log file.\r\n");
85         nResult = fclose(m_fp);
86         if (nResult == 0)
87             m_fp = 0;
88     }
89
90     return (nResult == 0);
91 }
92
93 BOOL LOGFILE::Write(const char *pszEntry, ...)
94 {
95     static BOOL bTimestampNextLine = TRUE;
96
97     if (!m_fp)
98         return FALSE;
99
100     if (bTimestampNextLine && (m_eTimeStampMode == TSM_EACH_ENTRY))
101         WriteTimeStamp();
102
103     va_list args;
104
105     va_start(args, pszEntry);
106
107     int nWritten = vfprintf(m_fp, pszEntry, args);
108
109     va_end(args);
110
111     fflush(m_fp);
112
113     // Don't timestamp next line unless current line ended with a newline
114     bTimestampNextLine = (pszEntry[strlen(pszEntry) - 1] == '\n');
115
116     return (nWritten > 0);
117 }
118
119 BOOL LOGFILE::WriteError(const char *pszMsg, DWORD nErrorCode, ...)
120 {
121     if (!m_fp)
122         return FALSE;
123
124     if (m_eTimeStampMode == TSM_EACH_ENTRY)
125         WriteTimeStamp();
126
127     va_list args;
128
129     va_start(args, nErrorCode);
130
131     int nWritten = vfprintf(m_fp, pszMsg, args);
132     va_end(args);
133
134     if (nWritten < 1)
135         return FALSE;
136
137     afs_status_t nStatus;
138     const char *pszErrorText;
139
140     int nResult = util_AdminErrorCodeTranslate(nErrorCode, TaLocale_GetLanguage(), &pszErrorText, &nStatus);
141     if (nResult)
142         fprintf(m_fp, ":  (0x%lx), %s.\r\n", nErrorCode, pszErrorText);
143     else
144         fprintf(m_fp, ":  (0x%lx).\r\n", nErrorCode);
145
146     fflush(m_fp);
147
148     return (nWritten > 0);
149 }
150
151 BOOL LOGFILE::WriteTimeStamp()
152 {
153     if (!m_fp)
154         return FALSE;
155
156     char szTime[64], szDate[64];
157
158     _strtime(szTime);
159     _strdate(szDate);
160
161     fprintf(m_fp, "%s %s:  ", szTime, szDate);
162
163     return TRUE;
164 }
165
166 BOOL LOGFILE::WriteBoolResult(BOOL bResult)
167 {
168     if (!m_fp)
169         return FALSE;
170
171     fprintf(m_fp, "%s.\r\n", bResult ? "Yes" : "No");
172
173     fflush(m_fp);
174
175     return TRUE;
176 }
177
178 BOOL LOGFILE::WriteMultistring(const char *pszMultiStr)
179 {
180     if (!m_fp)
181         return FALSE;
182
183     for (const char *p = pszMultiStr; *p; p += strlen(p))
184         Write("%s\r\n", p);
185
186     return TRUE;
187 }
188