summaryrefslogtreecommitdiff
blob: 1d0ee2ae3275e05acbd3d2ac7aa4d26756bc5435 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
diff --git a/src/init.cpp b/src/init.cpp
index 2dccc81..630fc29 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -8,6 +8,7 @@
 #include "net.h"
 #include "init.h"
 #include "strlcpy.h"
+#include "util.h"
 #include <boost/filesystem.hpp>
 #include <boost/filesystem/fstream.hpp>
 #include <boost/interprocess/sync/file_lock.hpp>
@@ -71,6 +72,10 @@ void HandleSIGTERM(int)
     fRequestShutdown = true;
 }
 
+void HandleSIGHUP(int)
+{
+    fReopenDebugLog = true;
+}
 
 
 
@@ -132,7 +137,13 @@ bool AppInit2(int argc, char* argv[])
     sa.sa_flags = 0;
     sigaction(SIGTERM, &sa, NULL);
     sigaction(SIGINT, &sa, NULL);
-    sigaction(SIGHUP, &sa, NULL);
+
+    // Reopen debug.log on SIGHUP
+    struct sigaction sa_hup;
+    sa_hup.sa_handler = HandleSIGHUP;
+    sigemptyset(&sa_hup.sa_mask);
+    sa_hup.sa_flags = 0;
+    sigaction(SIGHUP, &sa_hup, NULL);
 #endif
 
     //
diff --git a/src/util.cpp b/src/util.cpp
index 0f496bc..736fac6 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -30,6 +30,8 @@ string strMiscWarning;
 bool fTestNet = false;
 bool fNoListen = false;
 bool fLogTimestamps = false;
+FILE* fileout = NULL;
+bool fReopenDebugLog = false;
 
 
 
@@ -154,6 +156,13 @@ int GetRandInt(int nMax)
 
 
 
+string GetDebugLogName()
+{
+    char pszFile[MAX_PATH+100];
+    GetDataDir(pszFile);
+    strlcat(pszFile, "/debug.log", sizeof(pszFile));
+    return pszFile;
+}
 
 inline int OutputDebugStringF(const char* pszFormat, ...)
 {
@@ -169,19 +178,27 @@ inline int OutputDebugStringF(const char* pszFormat, ...)
     else
     {
         // print to debug.log
-        static FILE* fileout = NULL;
 
         if (!fileout)
         {
-            char pszFile[MAX_PATH+100];
-            GetDataDir(pszFile);
-            strlcat(pszFile, "/debug.log", sizeof(pszFile));
+            const char* pszFile = GetDebugLogName().c_str();
             fileout = fopen(pszFile, "a");
             if (fileout) setbuf(fileout, NULL); // unbuffered
         }
         if (fileout)
         {
             static bool fStartedNewLine = true;
+#ifndef WIN32
+            flockfile(fileout);
+
+            // reopen the log file, if requested
+            if (fReopenDebugLog) {
+                fReopenDebugLog = false;
+                const char* pszFile = GetDebugLogName().c_str();
+                if (freopen(pszFile,"a",fileout) != NULL)
+                    setbuf(fileout, NULL); // unbuffered
+            }
+#endif
 
             // Debug print useful for profiling
             if (fLogTimestamps && fStartedNewLine)
@@ -195,6 +212,9 @@ inline int OutputDebugStringF(const char* pszFormat, ...)
             va_start(arg_ptr, pszFormat);
             ret = vfprintf(fileout, pszFormat, arg_ptr);
             va_end(arg_ptr);
+#ifndef WIN32
+            funlockfile(fileout);
+#endif
         }
     }
 
diff --git a/src/util.h b/src/util.h
index 4e4cbb9..d2c19c9 100644
--- a/src/util.h
+++ b/src/util.h
@@ -166,6 +166,7 @@ extern std::string strMiscWarning;
 extern bool fTestNet;
 extern bool fNoListen;
 extern bool fLogTimestamps;
+extern bool fReopenDebugLog;
 
 void RandAddSeed();
 void RandAddSeedPerfmon();