Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add log

  • Loading branch information...
commit 4025d7c05adbcf6b461cbdcdd268da5c05fd3f77 1 parent 0504c03
@cloudwu authored
View
3  mk/build.mk
@@ -6,7 +6,8 @@ LIBSRCS := \
src/core/memory.c \
src/core/atom.c \
src/core/map.c \
- src/core/set.c
+ src/core/set.c \
+ src/core/log.c
include mk/build_o.mk
include mk/build_init.mk
View
10 mk/build_test.mk
@@ -4,7 +4,8 @@ test :
TESTSRCS := \
src/test/set.c \
- src/test/map.c
+ src/test/map.c \
+ src/test/log.c
INC := src/core
INC := $(addprefix -I,$(INC))
@@ -14,14 +15,15 @@ MKDIRS := $(MKDIRS) $(BUILD_TOP)/test $(TEST_DIR)
define TEST_temp
TAR_O := $(BUILD_TOP)/test/$(notdir $(basename $(1)))
$$(TAR_O).o : | $(BUILD_TOP)/test
- -include $$(TAR).d
+ -include $$(TAR_O).d
$$(TAR_O).o : $(1)
- $(CC) -c -o $$@ $(CFLAGS) $(INC) -MMD $$<
+ $(CC) -c -o $$@ $(CFLAGS) $(INC) -MMD $(1)
TAR_EXE := $(TEST_DIR)/$(notdir $(basename $(1)))$(EXE)
test : $$(TAR_EXE)
$$(TAR_EXE) : | $(TEST_DIR)
+ $$(TAR_EXE) : OBJS := $$(TAR_O).o
$$(TAR_EXE) : $$(TAR_O).o $(LIBTAR)
- $(CC) -o $$@ $(LDFLAGS) $$(TAR_O).o -L$(LIB_DIR) -lwindsoul
+ $(CC) -o $$@ $(LDFLAGS) $$(OBJS) -L$(LIB_DIR) -lwindsoul
endef
$(foreach s,$(TESTSRCS),$(eval $(call TEST_temp,$(s))))
View
54 src/core/log.c
@@ -0,0 +1,54 @@
+#include "log.h"
+#include "memory.h"
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#define MAX_SIZE 4096
+
+static void (*g_puts)(const char *string) = NULL;
+
+static void
+logv(const char *format, va_list ap)
+{
+ char * buffer = memoryTemp(MAX_SIZE);
+ vsnprintf(buffer, MAX_SIZE, format, ap);
+ g_puts(buffer);
+}
+
+void
+logPrintf(const char *format, ... )
+{
+ va_list ap;
+ va_start(ap,format);
+ logv(format, ap);
+ va_end(ap);
+}
+
+void
+logFatal(const char *format, ... )
+{
+ va_list ap;
+ va_start(ap,format);
+ logv(format, ap);
+ va_end(ap);
+ exit(1);
+}
+
+void logInject(void (*_puts)(const char *string))
+{
+ g_puts = _puts;
+}
+
+static void
+logerr(const char *string)
+{
+ fputs(string, stderr);
+}
+
+int
+logInit(void)
+{
+ logInject(logerr);
+ return 0;
+}
View
8 src/core/log.h
@@ -0,0 +1,8 @@
+#ifndef windsoul_log_h
+#define windsoul_log_h
+
+void logPrintf(const char *format, ... );
+void logFatal(const char *format, ... );
+void logInject(void (*puts)(const char *string));
+
+#endif
View
13 src/core/memory.c
@@ -32,3 +32,16 @@ memoryPermanent(size_t size)
return malloc(size);
}
+void *
+memoryTemp(size_t size)
+{
+ static void *temp = NULL;
+ static size_t sz = 0;
+ if (size > sz) {
+ sz = size;
+ free(temp);
+ temp = malloc(sz);
+ }
+ return temp;
+}
+
View
1  src/core/memory.h
@@ -7,5 +7,6 @@ void * memoryAlloc(size_t size);
void memoryFree(void *p);
void * memoryExpand(void *p, size_t osize, size_t nsize);
void * memoryPermanent(size_t size);
+void * memoryTemp(size_t size);
#endif
View
8 src/test/log.c
@@ -0,0 +1,8 @@
+#include "log.h"
+#include "test.h"
+
+void
+test()
+{
+ logPrintf("Hello World");
+}
View
14 src/test/map.c
@@ -1,9 +1,7 @@
#include <stdio.h>
#include "map.h"
#include "atom.h"
-
-int _mapInit(void);
-int _atomInit(void);
+#include "test.h"
static void
insert(struct map *m, const char *key, const char *value)
@@ -34,13 +32,3 @@ test()
mapRelease(m);
}
-int
-main()
-{
- if (_mapInit()) return 1;
- if (_atomInit()) return 1;
-
- test();
-
- return 0;
-}
View
14 src/test/set.c
@@ -1,9 +1,7 @@
#include <stdio.h>
#include "set.h"
#include "atom.h"
-
-int _setInit(void);
-int _atomInit(void);
+#include "test.h"
static void
test()
@@ -26,13 +24,3 @@ test()
setRelease(s);
}
-int
-main()
-{
- if (_setInit()) return 1;
- if (_atomInit()) return 1;
-
- test();
-
- return 0;
-}
View
25 src/test/test.h
@@ -0,0 +1,25 @@
+#ifndef windsoul_test_module_h
+#define windsoul_test_module_h
+
+static void test(void);
+
+int _setInit(void);
+int _atomInit(void);
+int _mapInit(void);
+int _logInit(void);
+
+int
+main()
+{
+ if (_setInit()) return 1;
+ if (_atomInit()) return 1;
+ if (_mapInit()) return 1;
+ if (_logInit()) return 1;
+
+ test();
+
+ return 0;
+}
+
+
+#endif
Please sign in to comment.
Something went wrong with that request. Please try again.