Skip to content

Commit

Permalink
Abstracted away logging functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Apr 1, 2024
1 parent 7dccfba commit dfdc54d
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 32 deletions.
30 changes: 30 additions & 0 deletions include/sk_log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#pragma once

#include <raylib.h>

#define SK_LOG_ENABLE_DEBUG_LEVEL SetTraceLogLevel(LOG_DEBUG)
#define SK_LOG_DEBUG(...) TraceLog(LOG_DEBUG, __VA_ARGS__)
#define SK_LOG_INFO(...) TraceLog(LOG_INFO, __VA_ARGS__)
#define SK_LOG_WARN(...) TraceLog(LOG_WARNING, __VA_ARGS__)
#define SK_LOG_ERROR(...) TraceLog(LOG_ERROR, __VA_ARGS__)
24 changes: 12 additions & 12 deletions src/sk_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <raylib.h>
#include <sk_log.h>
#include <sk_state.h>
#include <arpa/inet.h>
#include <sk_config.h>
Expand All @@ -32,20 +32,20 @@
#include <sk_renderer.h>

u8 sk_client_run(const char *ip) {
TraceLog(LOG_INFO, "Initializing %s", SK_CLIENT_NAME);
SK_LOG_INFO("Initializing %s", SK_CLIENT_NAME);
u8 is_online = 0;
int sock_fd;
if (!ip) TraceLog(LOG_WARNING, "Running in offline mode");
if (!ip) SK_LOG_WARN("Running in offline mode");
else {
TraceLog(LOG_INFO, "Connecting to `%s` ...", ip);
SK_LOG_INFO("Connecting to `%s` ...", ip);
const struct sockaddr_in server_addr = {
.sin_family = AF_INET,
.sin_port = htons(SK_SERVER_PORT),
.sin_addr.s_addr = inet_addr(ip)
};
sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock_fd == -1) {
TraceLog(LOG_ERROR, "socket(2) :: %s", strerror(errno));
SK_LOG_ERROR("socket(2) :: %s", strerror(errno));
return 1;
}
if (sendto(sock_fd,
Expand All @@ -54,14 +54,14 @@ u8 sk_client_run(const char *ip) {
0,
(const struct sockaddr *) &server_addr,
sizeof(server_addr)) == -1) {
TraceLog(LOG_ERROR, "sendto(2) :: %s", strerror(errno));
SK_LOG_ERROR("sendto(2) :: %s", strerror(errno));
close(sock_fd);
return 1;
}
char pong_msg[SK_SERVER_MSG_MAX_SIZE];
int pong_msg_n = recv(sock_fd, pong_msg, sizeof(pong_msg), MSG_WAITALL);
if (pong_msg_n == -1) {
TraceLog(LOG_ERROR, "recv(2) :: %s", strerror(errno));
SK_LOG_ERROR("recv(2) :: %s", strerror(errno));
close(sock_fd);
return 1;
}
Expand All @@ -70,14 +70,14 @@ u8 sk_client_run(const char *ip) {
is_online = 1;
}
if (!is_online) {
TraceLog(LOG_ERROR, "Unable to communicate with `%s`. Exiting...", ip);
SK_LOG_ERROR("Unable to communicate with `%s`. Exiting...", ip);
close(sock_fd);
return 1;
}
TraceLog(LOG_INFO, "Connected successfully to `%s`", ip);
SK_LOG_INFO("Connected successfully to `%s`", ip);
}
if (!ChangeDirectory(GetApplicationDirectory())) {
TraceLog(LOG_WARNING, "Could not change CWD to the game's root directory");
SK_LOG_WARN("Could not change CWD to the game's root directory");
}
sk_state state = sk_state_create(is_online);
if (!is_online) {
Expand All @@ -90,9 +90,9 @@ u8 sk_client_run(const char *ip) {
sk_renderer_destroy();
}
else {
TraceLog(LOG_WARNING, "Not implemented yet");
SK_LOG_WARN("Not implemented yet");
}
if (is_online) close(sock_fd);
TraceLog(LOG_INFO, "%s closed successfully", SK_CLIENT_NAME);
SK_LOG_INFO("%s closed successfully", SK_CLIENT_NAME);
return 0;
}
13 changes: 7 additions & 6 deletions src/sk_lobby.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/


#include <sk_log.h>
#include <sk_lobby.h>

sk_lobby sk_lobby_create(u8 id) {
Expand All @@ -41,7 +42,7 @@ void sk_lobby_destroy(sk_lobby *l) {

u8 sk_lobby_add(sk_lobby *l, sk_player p) {
if (l->players_count == SK_LOBBY_MAX_PLAYERS) {
TraceLog(LOG_DEBUG, "Lobby %u :: unable to add more players (full)", l->id);
SK_LOG_DEBUG("Lobby %u :: unable to add more players (full)", l->id);
return false;
}
for (u8 i = 0; i < SK_LOBBY_MAX_PLAYERS; ++i) {
Expand All @@ -50,10 +51,10 @@ u8 sk_lobby_add(sk_lobby *l, sk_player p) {
p.lobby_id = l->id;
p.lobby_slot_idx = i;
l->players[i] = p;
TraceLog(LOG_INFO, "Lobby %u :: added new player to slot %u/%u",
l->id,
i + 1,
SK_LOBBY_MAX_PLAYERS);
SK_LOG_INFO("Lobby %u :: added new player to slot %u/%u",
l->id,
i + 1,
SK_LOBBY_MAX_PLAYERS);
}
++l->players_count;
return true;
Expand All @@ -63,7 +64,7 @@ u8 sk_lobby_kick(sk_lobby *l, i8 p_idx) {
if (p_idx >= l->players_count ||
l->players[p_idx].lobby_id != l->id ||
l->players[p_idx].lobby_slot_idx != p_idx) {
TraceLog(LOG_DEBUG, "Lobby %u :: unable to kick non-existant player", l->id);
SK_LOG_DEBUG("Lobby %u :: unable to kick non-existant player", l->id);
return false;
}
l->players[p_idx].lobby_id = -1;
Expand Down
22 changes: 22 additions & 0 deletions src/sk_log.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* GNU Sparky --- A 5v5 character-based libre tactical shooter
* Copyright (C) 2024 Wasym A. Alonso
*
* This file is part of Sparky.
*
* Sparky is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sparky is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sparky. If not, see <http://www.gnu.org/licenses/>.
*/


#include <sk_log.h>
2 changes: 2 additions & 0 deletions src/sk_renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

void sk_renderer_create(void) {
SetConfigFlags(FLAG_MSAA_4X_HINT);
SetConfigFlags(FLAG_BORDERLESS_WINDOWED_MODE);
SetConfigFlags(FLAG_WINDOW_MOUSE_PASSTHROUGH);
InitWindow(SK_CONFIG_CLIENT_WIN_WIDTH,
SK_CONFIG_CLIENT_WIN_HEIGHT,
SK_CLIENT_NAME);
Expand Down
20 changes: 10 additions & 10 deletions src/sk_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <raylib.h>
#include <sk_log.h>
#include <sk_server.h>
#include <arpa/inet.h>
#include <sys/socket.h>
Expand Down Expand Up @@ -58,23 +58,23 @@ static void wait_next_tick(double t) {
}

u8 sk_server_run(void) {
TraceLog(LOG_INFO, "Initializing %s", SK_SERVER_NAME);
SK_LOG_INFO("Initializing %s", SK_SERVER_NAME);
const struct sockaddr_in server_addr = {
.sin_family = AF_INET,
.sin_port = htons(SK_SERVER_PORT),
.sin_addr.s_addr = inet_addr("127.0.0.1")
};
int sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock_fd == -1) {
TraceLog(LOG_ERROR, "socket(2) :: %s", strerror(errno));
SK_LOG_ERROR("socket(2) :: %s", strerror(errno));
return 1;
}
if (bind(sock_fd, (const struct sockaddr *) &server_addr, sizeof(server_addr)) == -1) {
TraceLog(LOG_ERROR, "bind(2) :: %s", strerror(errno));
SK_LOG_ERROR("bind(2) :: %s", strerror(errno));
close(sock_fd);
return 1;
}
TraceLog(LOG_INFO, "UDP socket binded to 127.0.0.1:%d", SK_SERVER_PORT);
SK_LOG_INFO("UDP socket binded to 127.0.0.1:%d", SK_SERVER_PORT);
double dt = 1 / SK_SERVER_TICK_RATE;
signal(SIGINT, handle_interrupt);
struct sockaddr_in client_addr;
Expand All @@ -93,12 +93,12 @@ u8 sk_server_run(void) {
(struct sockaddr *) &client_addr,
&client_addr_len);
if (msg_n == -1) {
TraceLog(LOG_ERROR, "recvfrom(2) :: %s", strerror(errno));
SK_LOG_ERROR("recvfrom(2) :: %s", strerror(errno));
continue;
}
msg[msg_n] = 0;
if (!strcmp(msg, SK_SERVER_MSG_CONN_REQ)) {
TraceLog(LOG_INFO, "Connection from client (%s:%d) requested", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
SK_LOG_INFO("Connection from client (%s:%d) requested", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// TODO: check if there is a free slot in a lobby
u8 lobby_id = 0;
u8 lobby_slot_id = 0;
Expand All @@ -111,14 +111,14 @@ u8 sk_server_run(void) {
0,
(struct sockaddr *) &client_addr,
client_addr_len) == -1) {
TraceLog(LOG_ERROR, "sendto(2) :: %s", strerror(errno));
SK_LOG_ERROR("sendto(2) :: %s", strerror(errno));
continue;
}
TraceLog(LOG_INFO, "Connection from client (%s:%d) accepted", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
SK_LOG_INFO("Connection from client (%s:%d) accepted", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
}
wait_next_tick(dt);
}
close(sock_fd);
TraceLog(LOG_INFO, "%s closed successfully", SK_SERVER_NAME);
SK_LOG_INFO("%s closed successfully", SK_SERVER_NAME);
return 0;
}
7 changes: 3 additions & 4 deletions src/sparky.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@

#include <stdio.h>
#include <string.h>
#include <raylib.h>
#include <sk_log.h>
#include <sk_text.h>
#include <sk_client.h>
#include <sk_server.h>

int main(int argc, char **argv) {
#ifndef NDEBUG
#define NBN_DEBUG
SetTraceLogLevel(LOG_DEBUG);
TraceLog(LOG_WARNING, "Running in debug mode");
SK_LOG_ENABLE_DEBUG_LEVEL;
SK_LOG_WARN("Running in debug mode");
#endif

if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
Expand Down

0 comments on commit dfdc54d

Please sign in to comment.