Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit eb89373

Browse files
authored
Merge pull request #1 from milosz275/base_program_structure
Base program structure
2 parents d62aff8 + 96af8d9 commit eb89373

File tree

13 files changed

+465
-26
lines changed

13 files changed

+465
-26
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,10 @@ CMakeCache.txt
4444

4545
# Other
4646
.notes/
47+
48+
# Logs
49+
*.log
50+
logs/
51+
52+
# VSCode
53+
*.vc.*

.vscode/settings.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"bitlab",
88
"Bitlab"
99
],
10+
"task.allowAutomaticTasks": "on",
1011
"editor.formatOnSave": true,
1112
"editor.codeActionsOnSave": {
1213
"source.fixAll.eslint": "always"
@@ -15,4 +16,17 @@
1516
"editor.formatOnType": true,
1617
"editor.formatOnPaste": true,
1718
"makefile.configureOnOpen": true,
19+
"files.associations": {
20+
"bitlab.h": "c"
21+
},
22+
"C_Cpp.formatting": "vcFormat",
23+
"C_Cpp.autoAddFileAssociations": true,
24+
"C_Cpp.default.browse.databaseFilename": "${workspaceFolder}/.vscode/browse.vc.db",
25+
"C_Cpp.vcFormat.space.pointerReferenceAlignment": "left",
26+
"C_Cpp.clang_format_style": "file",
27+
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}",
28+
"[cpp]": {
29+
"editor.formatOnType": true,
30+
"editor.formatOnSave": true
31+
}
1832
}

.vscode/tasks.json

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "git pull",
6+
"type": "shell",
7+
"command": "git pull",
8+
"problemMatcher": []
9+
},
10+
{
11+
"label": "build bitlab",
12+
"type": "shell",
13+
"command": "make",
14+
"options": {
15+
"cwd": "${workspaceFolder}/bitlab"
16+
},
17+
"group": {
18+
"kind": "build",
19+
"isDefault": true
20+
},
21+
"problemMatcher": []
22+
},
23+
{
24+
"label": "run bitlab",
25+
"type": "shell",
26+
"command": "bitlab/build/bin/main",
27+
"options": {
28+
"cwd": "${workspaceFolder}"
29+
},
30+
"problemMatcher": [],
31+
"isBackground": true,
32+
"group": {
33+
"kind": "none",
34+
"isDefault": false
35+
},
36+
"presentation": {
37+
"reveal": "always",
38+
"revealProblems": "onProblem",
39+
"panel": "dedicated",
40+
"showReuseMessage": false,
41+
"clear": false,
42+
"close": true
43+
},
44+
"runOptions": {
45+
"instanceLimit": 99999,
46+
"reevaluateOnRerun": false
47+
}
48+
},
49+
{
50+
"label": "build and run bitlab",
51+
"dependsOn": [
52+
"build bitlab",
53+
"run bitlab"
54+
],
55+
"dependsOrder": "sequence"
56+
},
57+
{
58+
"label": "clean all",
59+
"command": "make",
60+
"args": [
61+
"clean"
62+
],
63+
"problemMatcher": []
64+
},
65+
{
66+
"label": "clean all and build bitlab",
67+
"dependsOn": [
68+
"clean all",
69+
"build bitlab"
70+
],
71+
"dependsOrder": "sequence"
72+
},
73+
{
74+
"label": "delete database",
75+
"type": "shell",
76+
"command": "rm -rf *.db || rm -rf *.db-*",
77+
"options": {
78+
"cwd": "${workspaceFolder}/database"
79+
},
80+
"problemMatcher": []
81+
},
82+
{
83+
"label": "delete logs",
84+
"type": "shell",
85+
"command": "rm -rf *.log",
86+
"options": {
87+
"cwd": "${workspaceFolder}/logs"
88+
},
89+
"problemMatcher": []
90+
}
91+
]
92+
}

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ BitLab is an interactive command-line tool for exploring and interacting with th
1111

1212
BitLab acts as a powerful educational resource and debugging tool for understanding how Bitcoin nodes communicate, synchronize blocks, and process transactions.
1313

14+
- [GitHub Repo](https://github.com/milosz275/bitlab)
15+
- [Doxygen Docs](https://milosz275.github.io/bitlab)
16+
1417
## Table of Contents
1518

1619
## Getting Started
1720

21+
## Acknowledgements
22+
23+
- [Thread-safe logging](https://github.com/milosz275/secure-chat/blob/main/common/include/log.h)
24+
1825
## License
1926

2027
This project is licensed under the MIT License - see the [LICENSE](https://github.com/milosz275/bitlab/blob/main/LICENSE) file for details.

bitlab/Makefile

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
1-
# Compiler
21
CC = gcc
3-
4-
# Compiler flags
5-
CFLAGS = -std=c11 -Wall -Wextra
6-
7-
# Include directories
2+
CFLAGS = -std=c11 -Wall -Wextra -Werror -pedantic -fsanitize=address -O2 -Wno-unused-result
83
INCLUDES = -Iinclude
9-
10-
# Libraries
11-
CLIBS =
12-
13-
# Source files
4+
CLIBS = -lpthread -lreadline
145
CSRCS = $(wildcard src/*.c)
15-
16-
# Object files
176
COBJS = $(CSRCS:.c=.o)
187
COBJS := $(addprefix build/, $(COBJS))
19-
20-
# Executable name
218
MAIN = main
229

2310
.PHONY: default all debug clean depend
@@ -31,8 +18,6 @@ debug: CFLAGS += -g -DDEBUG -D_DEBUG
3118
debug: $(MAIN)
3219
@echo "Bitlab has been compiled in debug mode"
3320

34-
### Choose one of the following for C or C++ compilation -------------------
35-
3621
$(MAIN): $(COBJS)
3722
@mkdir -p build/bin
3823
$(CC) $(CFLAGS) $(INCLUDES) -o build/bin/$(MAIN) $(COBJS) $(CLIBS)
@@ -44,12 +29,9 @@ build/%.o: %.c
4429
depend: $(CSRCS)
4530
makedepend $(INCLUDES) $^
4631

47-
### -----------------------------------------------------------------------
48-
4932
clean:
5033
$(RM) build/src/*.o *~ $(MAIN)
5134
$(RM) build/bin/$(MAIN)
5235
$(RM) build/lib/*.a
5336

54-
# Include dependency files
5537
-include $(SRCS:.c=.d)

bitlab/include/bitlab.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef __BITLAB_H
2+
#define __BITLAB_H
3+
4+
typedef enum
5+
{
6+
BITLAB_RESULT_SUCCESS,
7+
BITLAB_RESULT_FAILURE
8+
} bitlab_result;
9+
10+
/**
11+
* Run the BitLab program.
12+
*
13+
* @param argc The number of arguments.
14+
* @param argv The arguments.
15+
* @return The result of the BitLab program.
16+
*/
17+
bitlab_result run_bitlab(int argc, char* argv[]);
18+
19+
#endif

bitlab/include/include.h

Lines changed: 0 additions & 4 deletions
This file was deleted.

bitlab/include/log.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#ifndef __LOG_H
2+
#define __LOG_H
3+
4+
#include <stdio.h>
5+
#include <stdarg.h>
6+
#include <pthread.h>
7+
8+
#define MAX_LOG_FILES 10
9+
#define MAX_FILENAME_LENGTH 256
10+
#define LOGS_DIR "logs"
11+
#define BITLAB_LOG "bitlab.log"
12+
#define LOG_BITLAB_STARTED "BitLab started ----------------------------------------------------------------------------------------"
13+
14+
#define LOCKED_FILE_RETRY_TIME 1000 // in microseconds (1 millisecond)
15+
#define LOCKED_FILE_TIMEOUT 5000000 // in microseconds (5 seconds)
16+
17+
/**
18+
* The log level enumeration used to define the level of logging that is being used.
19+
*
20+
* @param LOG_DEBUG Debug level logging
21+
* @param LOG_INFO Information level logging
22+
* @param LOG_WARN Warning level logging
23+
* @param LOG_ERROR Error level logging
24+
* @param LOG_FATAL Fatal level logging
25+
*/
26+
typedef enum
27+
{
28+
LOG_DEBUG,
29+
LOG_INFO,
30+
LOG_WARN,
31+
LOG_ERROR,
32+
LOG_FATAL
33+
} log_level;
34+
35+
/**
36+
* The logger structure used to store the logger for the logging system.
37+
*
38+
* @param filename The filename of the log file.
39+
* @param file The file pointer for the log file.
40+
*/
41+
typedef struct logger
42+
{
43+
char* filename;
44+
FILE* file;
45+
} logger;
46+
47+
/**
48+
* The loggers structure used to store the loggers for the logging system.
49+
*
50+
* @param log_mutex The mutex for the loggers.
51+
* @param array The array of loggers.
52+
* @param is_initializing The flag to indicate if the loggers are initializing.
53+
*/
54+
typedef struct loggers
55+
{
56+
pthread_mutex_t log_mutex;
57+
logger* array[MAX_LOG_FILES];
58+
int is_initializing;
59+
} loggers;
60+
61+
/**
62+
* Initialize logging used to initialize the logging system, open and preserve the log file.
63+
*
64+
* @param log_file The log file to write to.
65+
*/
66+
void init_logging(const char* filename);
67+
68+
/**
69+
* Log a message used to log a message to the console or a file.
70+
*
71+
* @param level The log level.
72+
* @param log_file The file that the log message is destined for.
73+
* @param source_file The source file that the log message is from.
74+
* @param format The format of the log message.
75+
* @param ... The arguments for the format.
76+
*/
77+
void log_message(log_level level, const char* filename, const char* source_file, const char* format, ...);
78+
79+
/**
80+
* Finish logging used to finish logging and close the log file.
81+
*/
82+
void finish_logging();
83+
84+
#endif

bitlab/include/utils.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef __UTILS_H
2+
#define __UTILS_H
3+
4+
#include <stddef.h>
5+
6+
#define TIMESTAMP_LENGTH 20
7+
8+
/**
9+
* Get the timestamp. This function is used to get the timestamp in a YYYYMMDDHHMMSS format.
10+
*
11+
* @param buffer The buffer to store the timestamp.
12+
* @param buffer_size The size of the buffer.
13+
*/
14+
void get_timestamp(char* buffer, size_t buffer_size);
15+
16+
/**
17+
* Get the formatted timestamp. This function is used to get the formatted timestamp in a YYYY-MM-DD HH:MM:SS format.
18+
*
19+
* @param buffer The buffer to store the formatted timestamp.
20+
* @param buffer_size The size of the buffer.
21+
*/
22+
void get_formatted_timestamp(char* buffer, size_t buffer_size);
23+
24+
#endif

bitlab/src/bitlab.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "bitlab.h"
2+
3+
#include <stdio.h>
4+
5+
#include "log.h"
6+
7+
bitlab_result run_bitlab(int argc, char* argv[])
8+
{
9+
// [ ] Utilize the argc and argv parameters.
10+
if (argc != 1 && argv != NULL) {}
11+
12+
init_logging(BITLAB_LOG);
13+
log_message(LOG_INFO, BITLAB_LOG, __FILE__, LOG_BITLAB_STARTED);
14+
15+
16+
printf("BitLab is running!\n");
17+
18+
finish_logging();
19+
return BITLAB_RESULT_SUCCESS;
20+
}

0 commit comments

Comments
 (0)