1
1
Fork 0
Browse Source

moved logger

thread-physics
alistair 5 years ago
parent
commit
645146c669
  1. 9
      Makefile
  2. BIN
      a.out
  3. 64
      logger.c
  4. 15
      logger.h
  5. BIN
      logger.h.gch
  6. BIN
      logger.o
  7. BIN
      main
  8. 69
      main.c
  9. BIN
      main.o

9
Makefile

@ -8,11 +8,14 @@ run: all @@ -8,11 +8,14 @@ run: all
all: $(EXE)
$(EXE): main.o
$(CXX) $(CXXFLAGS) $< -o $@
$(EXE): main.o logger.o
$(CXX) $(CXXFLAGS) -o $(EXE) main.o logger.o
main.o: main.c
main.o: main.c logger.h
$(CXX) $(CXXFLAGS) -c main.c
logger.o: logger.c logger.h
$(CXX) $(CXXFLAGS) -c logger.c
clean:

BIN
a.out

Binary file not shown.

64
logger.c

@ -0,0 +1,64 @@ @@ -0,0 +1,64 @@
#include "logger.h"
// Defaults
loggerlevel LOGLEVEL = DEBUG;
loggerlevel STDOUTLEVEL = INFO;
int copy_str(char * out_string, char * string) {
/* A drop in replacement for strcpy() from <string.h>*/
int len = 0;
while (*(string + len) != '\0') {
*(out_string + len) = *(string + len);
len++;
}
*(out_string + len) = '\0';
return (1);
}
void logwrite(loggerlevel level, char message[]) {
// write a string message
char time_str[100];
time_t current_time;
time(&current_time);
copy_str(time_str, asctime(localtime(&current_time)));
// delete the \n
time_str[24] = ' ';
char prepend[15];
copy_str(prepend, time_str);
switch (level) {
case DEBUG:
strcat(prepend, "DEBUG");
break;
case INFO:
strcat(prepend, "INFO ");
break;
case WARN:
strcat(prepend, "WARN ");
break;
case ERROR:
strcat(prepend, "ERROR");
break;
}
strcat(prepend, ": ");
if (level <= LOGLEVEL) {
FILE * logfile = fopen("debug.log", "a");
fprintf(logfile, "%s", prepend);
fprintf(logfile, message);
fclose(logfile);
}
if (level <= STDOUTLEVEL) {
printf("%s", prepend);
printf(message);
}
}

15
logger.h

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
#include <time.h>
#include <string.h>
#include <stdio.h>
typedef enum {
// high-low priority
// low-high verbosity
SILENT, ERROR, WARN, INFO, DEBUG
} loggerlevel;
extern loggerlevel LOGLEVEL;
extern loggerlevel STDOUTLEVEL;
void logwrite(loggerlevel level, char message[]);

BIN
logger.h.gch

Binary file not shown.

BIN
logger.o

Binary file not shown.

BIN
main

Binary file not shown.

69
main.c

@ -5,67 +5,17 @@ @@ -5,67 +5,17 @@
#include <SDL2/SDL_image.h>
#include <string.h>
#include <time.h>
#include "basic-lib.c"
#include "logger.h"
const int screen_width = 800;
const int screen_height = 600;
/* logging definition */
typedef enum {
// high-low priority
// low-high verbosity
SILENT, ERROR, WARN, INFO, DEBUG
} logger_level;
logger_level LOGLEVEL = DEBUG;
logger_level STDOUTLEVEL = DEBUG;
void logwrite(logger_level level, char message[]) {
// write a string message
char time_str[100];
time_t current_time;
time(&current_time);
copy_str(time_str, asctime(localtime(&current_time)));
// delete the \n
time_str[24] = ' ';
char prepend[15];
copy_str(prepend, time_str);
switch (level) {
case DEBUG:
strcat(prepend, "DEBUG");
break;
case INFO:
strcat(prepend, "INFO ");
break;
case WARN:
strcat(prepend, "WARN ");
break;
case ERROR:
strcat(prepend, "ERROR");
break;
}
strcat(prepend, ": ");
if (level <= LOGLEVEL) {
FILE * logfile = fopen("debug.log", "a");
fprintf(logfile, "%s", prepend);
fprintf(logfile, message);
fclose(logfile);
}
if (level <= STDOUTLEVEL) {
printf("%s", prepend);
printf(message);
}
//LOGLEVEL = DEBUG;
//STDOUTLEVEL = DEBUG;
}
/* Type definition for object cleanups */
typedef enum {
NO, SURFACE, TEXTURE, RENDERER, WINDOW
@ -144,7 +94,6 @@ void empty_cleanup_queue(void) { @@ -144,7 +94,6 @@ void empty_cleanup_queue(void) {
}
}
struct SDL_Window* make_window(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
@ -156,12 +105,17 @@ struct SDL_Window* make_window(void) { @@ -156,12 +105,17 @@ struct SDL_Window* make_window(void) {
}
void step(void) {
// game
// calc time interval
//
// draw
// advance game physics
return;
}
void redraw(void) {
// check time
// redraw if 1/60th of second passed
}
void render_texture_at(struct SDL_Renderer * ren, struct SDL_Texture * texture,int x, int y) {
SDL_Rect destination;
@ -183,7 +137,6 @@ void refresh(struct SDL_Renderer * ren) { @@ -183,7 +137,6 @@ void refresh(struct SDL_Renderer * ren) {
}
SDL_Texture * load_image(SDL_Renderer * ren, char fname[]) {
SDL_Surface * surface = IMG_Load(fname) ;
if(!surface) {

BIN
main.o

Binary file not shown.
Loading…
Cancel
Save