Debian Codegraph @

dokidoki-support/log.c

raw symbols SCIP
1 #include "log.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include <lauxlib.h>
8 #include <lualib.h>
9
10 FILE *file = NULL;
11
12 #if defined(DOKIDOKI_LINUX) || defined(DOKIDOKI_MACOSX)
13 static void open_logfile()
14 {
15 const char *suffix = "/.dokidoki.log";
16 const char *home = getenv("HOME");
17 size_t len = strlen(suffix) + strlen(home) + 1;
18 char *path = (char *)malloc(len * sizeof(char));
19 strcpy(path, home);
20 strcat(path, suffix);
21 file = fopen(path, "w");
22 free(path);
23 }
24 #else
25 static void open_logfile()
26 {
27 file = fopen("dokidoki.log", "w");
28 }
29 #endif
30
31 void log_init(int use_logfile)
32 {
33 if(use_logfile)
34 open_logfile();
35
36 if(!file)
37 file = stderr;
38 }
39
40 void log_message(const char *message)
41 {
42 fputs(message, file);
43 fputs("\n", file);
44 fflush(file);
45 }
46
47 void log_messagef(const char *fmt, ...)
48 {
49 va_list args;
50 va_start(args, fmt);
51
52 vfprintf(file, fmt, args);
53 fputs("\n", file);
54 fflush(file);
55
56 va_end(args);
57 }
58
59 //// lua binding //////////////////////////////////////////////////////////////
60
61 static int log__log_message(lua_State *L)
62 {
63 log_message(luaL_checkstring(L, 1));
64 return 0;
65 }
66
67 static const luaL_Reg log_lib[] =
68 {
69 {"log_message", log__log_message},
70 {NULL, NULL}
71 };
72
73 int luaopen_log(lua_State *L)
74 {
75 lua_newtable(L);
76 luaL_register(L, NULL, log_lib);
77 return 1;
78 }