Skip to content

Commit

Permalink
feat(server): add/change some log messages
Browse files Browse the repository at this point in the history
chore(config): add default_conf member
chore(database): add returning value to create_cache method
  • Loading branch information
aloima committed Oct 4, 2024
1 parent 8af6717 commit ac71890
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
2 changes: 2 additions & 0 deletions headers/config.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdint.h>
#include <stdbool.h>

#ifndef CONFIG_H
#define CONFIG_H
Expand All @@ -10,6 +11,7 @@
uint32_t max_log_len;
char data_file[49];
char log_file[49];
bool default_conf;
};

struct Configuration *get_configuration(const char *filename);
Expand Down
2 changes: 1 addition & 1 deletion headers/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
off_t pos;
};

void create_cache();
struct BTree *create_cache();
struct BTree *get_cache();
struct KVPair *get_kv_from_cache(const char *key);
void free_cache();
Expand Down
4 changes: 2 additions & 2 deletions src/database/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

static struct BTree *cache = NULL;

void create_cache() {
cache = create_btree(3);
struct BTree *create_cache() {
return (cache = create_btree(3));
}

struct BTree *get_cache() {
Expand Down
17 changes: 13 additions & 4 deletions src/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdbool.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>

#include <pthread.h>
#include <fcntl.h>
Expand Down Expand Up @@ -59,10 +60,10 @@ static void close_server() {
remove_client(client->connfd);
}

write_log(LOG_WARN, "Saving data...");
clock_t start = clock();
save_data();
close_database_fd();
write_log(LOG_INFO, "Saved data and closed database file.");
write_log(LOG_INFO, "Saved data and closed database file in %.2f seconds.", ((float) clock() - start) / CLOCKS_PER_SEC);

deactive_transaction_thread();
usleep(15);
Expand Down Expand Up @@ -90,6 +91,14 @@ void start_server(struct Configuration *config) {
conf = config;
initialize_logs(conf);
write_log(LOG_INFO, "Initialized logs and configuration.");
write_log(LOG_INFO, "version=" VERSION ", commit hash=" GIT_HASH);

if (conf->default_conf) {
write_log(LOG_WARN, (
"There is no configuration file, using default configuration. "
"To specify, create .tellyconf file or use `telly config /path/to/file`."
));
}

load_commands();
write_log(LOG_INFO, "Initialized commands.");
Expand Down Expand Up @@ -144,12 +153,12 @@ void start_server(struct Configuration *config) {
return;
}

create_cache();
struct BTree *cache = create_cache();
open_database_fd(conf->data_file);
write_log(LOG_INFO, "Created cache and opened database file.");

get_all_keys();
write_log(LOG_INFO, "Read database file to create keyspace.");
write_log(LOG_INFO, "Read database file to create keyspace. Loaded key count: %d", cache->size);

nfds = 1;
fds = malloc(sizeof(struct pollfd));
Expand Down
3 changes: 2 additions & 1 deletion src/utils/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ static struct Configuration default_conf = {
.allowed_log_levels = LOG_INFO | LOG_ERR | LOG_WARN,
.max_log_len = 8192,
.data_file = ".tellydb",
.log_file = ".tellylog"
.log_file = ".tellylog",
.default_conf = true
};

static void pass_line(FILE *file, char c) {
Expand Down

0 comments on commit ac71890

Please sign in to comment.