summaryrefslogtreecommitdiff
path: root/toxcore/util.c
diff options
context:
space:
mode:
authorCoren[m] <Break@Ocean>2013-09-21 01:27:53 +0200
committerCoren[m] <Break@Ocean>2013-09-21 01:27:53 +0200
commit89005f17018fcf829efee3cd243c84ef330becfc (patch)
tree7f4f584e810a5de0c637c066e13a5778a5dc02ad /toxcore/util.c
parent13bd6aab187d481e51f45f74f64f5b92c7acf935 (diff)
parent20b6900fb181f71eca4577a5f1e1f5f3ecd6a28b (diff)
Merge branch 'master' of https://github.com/irungentoo/ProjectTox-Core.git into ipv6.yield50%toipv4
Diffstat (limited to 'toxcore/util.c')
-rw-r--r--toxcore/util.c52
1 files changed, 49 insertions, 3 deletions
diff --git a/toxcore/util.c b/toxcore/util.c
index b3263d05..7b2735a0 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -94,6 +94,9 @@ int load_state(load_state_callback_func load_state_callback, void *outer,
94 94
95#ifdef LOGGING 95#ifdef LOGGING
96time_t starttime = 0; 96time_t starttime = 0;
97size_t logbufferprelen = 0;
98char *logbufferpredata = NULL;
99char *logbufferprehead = NULL;
97char logbuffer[512]; 100char logbuffer[512];
98static FILE *logfile = NULL; 101static FILE *logfile = NULL;
99void loginit(uint16_t port) 102void loginit(uint16_t port)
@@ -101,9 +104,23 @@ void loginit(uint16_t port)
101 if (logfile) 104 if (logfile)
102 fclose(logfile); 105 fclose(logfile);
103 106
104 sprintf(logbuffer, "%u-%u.log", ntohs(port), (uint32_t)now()); 107 if (!starttime)
108 starttime = now();
109
110 struct tm *tm = localtime(&starttime);
111 if (strftime(logbuffer + 32, sizeof(logbuffer) - 32, "%F %T", tm))
112 sprintf(logbuffer, "%u-%s.log", ntohs(port), logbuffer + 32);
113 else
114 sprintf(logbuffer, "%u-%lu.log", ntohs(port), starttime);
105 logfile = fopen(logbuffer, "w"); 115 logfile = fopen(logbuffer, "w");
106 starttime = now(); 116 if (logbufferpredata) {
117 if (logfile)
118 fprintf(logfile, logbufferpredata);
119
120 free(logbufferpredata);
121 logbufferpredata = NULL;
122 }
123
107}; 124};
108void loglog(char *text) 125void loglog(char *text)
109{ 126{
@@ -111,8 +128,37 @@ void loglog(char *text)
111 fprintf(logfile, "%4u ", (uint32_t)(now() - starttime)); 128 fprintf(logfile, "%4u ", (uint32_t)(now() - starttime));
112 fprintf(logfile, text); 129 fprintf(logfile, text);
113 fflush(logfile); 130 fflush(logfile);
131
132 return;
114 } 133 }
115}; 134
135 /* log messages before file was opened: store */
136
137 size_t len = strlen(text);
138 if (!starttime) {
139 starttime = now();
140 logbufferprelen = 1024 + len - (len % 1024);
141 logbufferpredata = malloc(logbufferprelen);
142 logbufferprehead = logbufferpredata;
143 }
144
145 /* loginit() called meanwhile? (but failed to open) */
146 if (!logbufferpredata)
147 return;
148
149 if (len + logbufferprehead - logbufferpredata + 16U < logbufferprelen) {
150 size_t logpos = logbufferprehead - logbufferpredata;
151 size_t lennew = logbufferprelen * 1.4;
152 logbufferpredata = realloc(logbufferpredata, lennew);
153 logbufferprehead = logbufferpredata + logpos;
154 logbufferprelen = lennew;
155 }
156
157 size_t written;
158 sprintf(logbufferprehead, "%4u %s%n", (uint32_t)(now() - starttime), text, &written);
159 logbufferprehead += written;
160}
161
116void logexit() 162void logexit()
117{ 163{
118 if (logfile) { 164 if (logfile) {