Added in logging on the C-side, still need to do for lua
This commit is contained in:
		
							parent
							
								
									bfd1162cdc
								
							
						
					
					
						commit
						6a13c8d00e
					
				
							
								
								
									
										8
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								Makefile
									
									
									
									
									
								
							@ -1,11 +1,11 @@
 | 
				
			|||||||
CC=cc
 | 
					CC=gcc
 | 
				
			||||||
CLIB=-L/usr/local/lib -llua5.3
 | 
					CLIB=-L/usr/local/lib -llua5.3
 | 
				
			||||||
INC=-I/usr/local/include
 | 
					INC=-I/usr/local/include
 | 
				
			||||||
BIN=main
 | 
					BIN=open_card_table
 | 
				
			||||||
DEBUG=-g
 | 
					DEBUG=-g
 | 
				
			||||||
 | 
					
 | 
				
			||||||
open_card_table: main.o oct_networking.o
 | 
					open_card_table: main.o oct_networking.o oct_log.o
 | 
				
			||||||
	$(CC) -g $(INC) $(CLIB) -o open_card_table main.o oct_networking.o
 | 
						$(CC) -g $(INC) $(CLIB) -o $(BIN) $^
 | 
				
			||||||
 | 
					
 | 
				
			||||||
%.o: %.c
 | 
					%.o: %.c
 | 
				
			||||||
	$(CC) -g $(INC) $(CLIB) -c -o $@ $<
 | 
						$(CC) -g $(INC) $(CLIB) -c -o $@ $<
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										78
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										78
									
								
								main.c
									
									
									
									
									
								
							@ -4,6 +4,7 @@
 | 
				
			|||||||
#include "termbox_render.h"
 | 
					#include "termbox_render.h"
 | 
				
			||||||
#include "oct_termbox_sprite.h"
 | 
					#include "oct_termbox_sprite.h"
 | 
				
			||||||
#include "oct_networking.h"
 | 
					#include "oct_networking.h"
 | 
				
			||||||
 | 
					#include "oct_log.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define TB_IMPL
 | 
					#define TB_IMPL
 | 
				
			||||||
#include "termbox.h"
 | 
					#include "termbox.h"
 | 
				
			||||||
@ -16,7 +17,7 @@
 | 
				
			|||||||
#define OCT_VERS 5
 | 
					#define OCT_VERS 5
 | 
				
			||||||
#define OCT_INVALID_ARGUMENT 6
 | 
					#define OCT_INVALID_ARGUMENT 6
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define OCT_LUA_FILENAME_SIZE 128
 | 
					#define OCT_MAX_FILENAME_SIZE 1024
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define OCT_VERSION "0.0"
 | 
					#define OCT_VERSION "0.0"
 | 
				
			||||||
#define OCT_URL "https://git.thejerks.club/j4nk/open-card-table"
 | 
					#define OCT_URL "https://git.thejerks.club/j4nk/open-card-table"
 | 
				
			||||||
@ -30,7 +31,7 @@ struct {
 | 
				
			|||||||
	char port[6]; // max 65535 so 6 bytes needed
 | 
						char port[6]; // max 65535 so 6 bytes needed
 | 
				
			||||||
	// Might come in handy later to keep filename
 | 
						// Might come in handy later to keep filename
 | 
				
			||||||
	// e.g. if I ever want to allow downloading a script from peer
 | 
						// e.g. if I ever want to allow downloading a script from peer
 | 
				
			||||||
	char lua_file[OCT_LUA_FILENAME_SIZE];
 | 
						char lua_file[OCT_MAX_FILENAME_SIZE];
 | 
				
			||||||
} args;
 | 
					} args;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void usage();
 | 
					void usage();
 | 
				
			||||||
@ -43,15 +44,19 @@ int main(int argc, char* argv[]) {
 | 
				
			|||||||
	int process_args_result = process_args(argc, argv);
 | 
						int process_args_result = process_args(argc, argv);
 | 
				
			||||||
	switch (process_args_result) {
 | 
						switch (process_args_result) {
 | 
				
			||||||
	case OCT_NO_LUA_FILE:
 | 
						case OCT_NO_LUA_FILE:
 | 
				
			||||||
 | 
							// Can't use oct_log as logging is not guaranteed to be init
 | 
				
			||||||
		fprintf(stderr, "Error: No lua file given\n\n");
 | 
							fprintf(stderr, "Error: No lua file given\n\n");
 | 
				
			||||||
		usage();
 | 
							usage();
 | 
				
			||||||
		return EXIT_FAILURE;
 | 
							return EXIT_FAILURE;
 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
	case OCT_LUA_FILE_NOT_FOUND:
 | 
						case OCT_LUA_FILE_NOT_FOUND:
 | 
				
			||||||
 | 
							// oct_log is init by this time, but don't want PITA bug in case this changes
 | 
				
			||||||
		fprintf(stderr, "Error: Could not open file: %s\n", argv[argc-1]);
 | 
							fprintf(stderr, "Error: Could not open file: %s\n", argv[argc-1]);
 | 
				
			||||||
 | 
							deinitialize_everything();
 | 
				
			||||||
		return EXIT_FAILURE;
 | 
							return EXIT_FAILURE;
 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
	case OCT_INVALID_ARGUMENT:
 | 
						case OCT_INVALID_ARGUMENT:
 | 
				
			||||||
 | 
							// Can't use oct_log as logging is not guaranteed to be init
 | 
				
			||||||
		fprintf(stderr, "Error: unknown argument\n");
 | 
							fprintf(stderr, "Error: unknown argument\n");
 | 
				
			||||||
		return EXIT_FAILURE;
 | 
							return EXIT_FAILURE;
 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
@ -93,15 +98,23 @@ int process_args(int argc, char* argv[]) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// Set args.port to default
 | 
						// Set args.port to default
 | 
				
			||||||
	strncpy(args.port, OCT_DEFAULT_PORT, 6);
 | 
						strncpy(args.port, OCT_DEFAULT_PORT, 6);
 | 
				
			||||||
 | 
						int log_level = OCT_LOG_LEVEL_ERROR;
 | 
				
			||||||
 | 
						char log_file[OCT_MAX_FILENAME_SIZE];
 | 
				
			||||||
 | 
						int log_file_spec = 0;
 | 
				
			||||||
 | 
						int valid_last_argument = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for (int i = 1; i < argc; i++) {
 | 
						for (int i = 1; i < argc; i++) {
 | 
				
			||||||
		// argv's are guaranteed to be zero-terminated, so can use
 | 
							// argv's are guaranteed to be zero-terminated, so can use
 | 
				
			||||||
		// strcmp instead of strncmp
 | 
							// strcmp instead of strncmp
 | 
				
			||||||
		if (strcmp(argv[i], "-p") == 0) {
 | 
							if (strcmp(argv[i], "-p") == 0) {
 | 
				
			||||||
			if (i != (argc-2)) {
 | 
								if (i+1 < argc) {
 | 
				
			||||||
				strncpy(args.port, argv[i+1], 6);
 | 
									strncpy(args.port, argv[i+1], 6);
 | 
				
			||||||
				i++;
 | 
									i++;
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
								else {
 | 
				
			||||||
 | 
									fprintf(stderr, "Error: need to specify a port after -p\n");
 | 
				
			||||||
 | 
									return OCT_INVALID_ARGUMENT;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else if (strcmp(argv[i], "-h") == 0) {
 | 
							else if (strcmp(argv[i], "-h") == 0) {
 | 
				
			||||||
			return OCT_HELP;
 | 
								return OCT_HELP;
 | 
				
			||||||
@ -109,19 +122,53 @@ int process_args(int argc, char* argv[]) {
 | 
				
			|||||||
		else if (strcmp(argv[i], "-v") == 0) {
 | 
							else if (strcmp(argv[i], "-v") == 0) {
 | 
				
			||||||
			return OCT_VERS;
 | 
								return OCT_VERS;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							else if (strcmp(argv[i], "-ll") == 0) {
 | 
				
			||||||
 | 
								if (i+1 < argc) {
 | 
				
			||||||
 | 
									char* endptr = NULL;
 | 
				
			||||||
 | 
									log_level = strtoul(argv[i+1], &endptr, 10);
 | 
				
			||||||
 | 
									if (log_level == 0 && endptr == argv[i+1]) {
 | 
				
			||||||
 | 
										printf("Error: invalid log level\n");
 | 
				
			||||||
 | 
										return OCT_INVALID_ARGUMENT;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									i++;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			else {
 | 
								else {
 | 
				
			||||||
 | 
									fprintf(stderr, "Error: need to specify a log level after -ll\n");
 | 
				
			||||||
 | 
									return OCT_INVALID_ARGUMENT;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							else if (strcmp(argv[i], "-lf") == 0) {
 | 
				
			||||||
 | 
								if (i+1 < argc) {
 | 
				
			||||||
 | 
									strncpy(log_file, argv[i+1], OCT_MAX_FILENAME_SIZE);
 | 
				
			||||||
 | 
									log_file_spec = 1;
 | 
				
			||||||
 | 
									i++;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								else {
 | 
				
			||||||
 | 
									fprintf(stderr, "Error: need to specify a log file after -lf\n");
 | 
				
			||||||
 | 
									return OCT_INVALID_ARGUMENT;				
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							else {
 | 
				
			||||||
 | 
								// This is the lua file to run
 | 
				
			||||||
			if (i == argc-1) {
 | 
								if (i == argc-1) {
 | 
				
			||||||
 | 
									valid_last_argument = 1;
 | 
				
			||||||
				break;
 | 
									break;
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
								// Invalid argument
 | 
				
			||||||
			else {
 | 
								else {
 | 
				
			||||||
				return OCT_INVALID_ARGUMENT;
 | 
									return OCT_INVALID_ARGUMENT;
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if (!valid_last_argument) {
 | 
				
			||||||
 | 
							return OCT_NO_LUA_FILE;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	strncpy(args.lua_file, argv[argc-1], OCT_LUA_FILENAME_SIZE);
 | 
						oct_log_init(log_file_spec ? log_file : NULL, log_level);
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						strncpy(args.lua_file, argv[argc-1], OCT_MAX_FILENAME_SIZE);
 | 
				
			||||||
 | 
						OCT_LOG_INFO("Running lua file: %s", args.lua_file);
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	// TODO define other cmd line args here
 | 
					 | 
				
			||||||
	return initialize_everything(argv[argc-1]); // lua file should always be last argument
 | 
						return initialize_everything(argv[argc-1]); // lua file should always be last argument
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -133,20 +180,25 @@ int initialize_everything(char* lua_file) {
 | 
				
			|||||||
	if (!oct_tb_sprite_list_initialize()) {
 | 
						if (!oct_tb_sprite_list_initialize()) {
 | 
				
			||||||
		return 0;
 | 
							return 0;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						OCT_LOG_INFO("Initialized the sprite list");
 | 
				
			||||||
	L = luaL_newstate();
 | 
						L = luaL_newstate();
 | 
				
			||||||
	if (L == NULL) {
 | 
						if (L == NULL) {
 | 
				
			||||||
		fprintf(stderr, "Can't initialize Lua\n");
 | 
							OCT_LOG_ERROR("Can't initialize Lua\n");
 | 
				
			||||||
		return 0;
 | 
							return 0;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						OCT_LOG_INFO("Initialized lua state");
 | 
				
			||||||
	luaL_openlibs(L);
 | 
						luaL_openlibs(L);
 | 
				
			||||||
	oct_tb_initialize_lua(L);
 | 
						oct_tb_initialize_lua(L);
 | 
				
			||||||
	//if (luaL_dofile(L, lua_file)) return OCT_LUA_FILE_NOT_FOUND;
 | 
						if (luaL_dofile(L, lua_file)) return OCT_LUA_FILE_NOT_FOUND;
 | 
				
			||||||
 | 
						OCT_LOG_INFO("Begin running oct_init()");
 | 
				
			||||||
	lua_getglobal(L, "oct_init");
 | 
						lua_getglobal(L, "oct_init");
 | 
				
			||||||
	lua_call(L, 0, 1);
 | 
						lua_call(L, 0, 1);
 | 
				
			||||||
	int type = lua_tointeger(L, -1);
 | 
						int type = lua_tointeger(L, -1);
 | 
				
			||||||
 | 
						OCT_LOG_INFO("Finish running oct_init()");
 | 
				
			||||||
	if (type == OCT_TYPE_SERVER) {
 | 
						if (type == OCT_TYPE_SERVER) {
 | 
				
			||||||
 | 
							OCT_LOG_INFO("Lua script is server type");
 | 
				
			||||||
		if (!oct_network_server_init(args.port)) {
 | 
							if (!oct_network_server_init(args.port)) {
 | 
				
			||||||
			printf("Could not establish a socket on port %s\n", args.port);
 | 
								OCT_LOG_ERROR("Could not establish a socket on port %s\n", args.port);
 | 
				
			||||||
			return 0;
 | 
								return 0;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -162,9 +214,11 @@ int initialize_everything(char* lua_file) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int deinitialize_everything() {
 | 
					int deinitialize_everything() {
 | 
				
			||||||
 | 
						OCT_LOG_INFO("Deinitializing everything");
 | 
				
			||||||
	tb_shutdown();
 | 
						tb_shutdown();
 | 
				
			||||||
	lua_close(L);
 | 
						lua_close(L);
 | 
				
			||||||
	oct_tb_sprite_list_deinitialize();
 | 
						oct_tb_sprite_list_deinitialize();
 | 
				
			||||||
 | 
						oct_log_deinit();
 | 
				
			||||||
	return 1;
 | 
						return 1;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -174,9 +228,11 @@ void usage() {
 | 
				
			|||||||
	fprintf(stderr, "\n");
 | 
						fprintf(stderr, "\n");
 | 
				
			||||||
	fprintf(stderr, "Usage: ./open_card_table [OPTIONS] FILE\n");
 | 
						fprintf(stderr, "Usage: ./open_card_table [OPTIONS] FILE\n");
 | 
				
			||||||
	fprintf(stderr, "\n");
 | 
						fprintf(stderr, "\n");
 | 
				
			||||||
	fprintf(stderr, "  -p\tPort on which to listen\n");
 | 
						fprintf(stderr, "  -p\t\tPort on which to listen\n");
 | 
				
			||||||
	fprintf(stderr, "  -v\tPrint version and exit\n");
 | 
						fprintf(stderr, "  -v\t\tPrint version and exit\n");
 | 
				
			||||||
	fprintf(stderr, "  -h\tPrint this message and exit\n");
 | 
						fprintf(stderr, "  -h\t\tPrint this message and exit\n");
 | 
				
			||||||
 | 
						fprintf(stderr, "  -ll\t\t Set log level: 0=err, 1=warn, 2=info, 3=debug\n");
 | 
				
			||||||
 | 
						fprintf(stderr, "  -lf\t\t Specify log output file\n");
 | 
				
			||||||
	fprintf(stderr, "\n");
 | 
						fprintf(stderr, "\n");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user