Skip to content

Commit

Permalink
Fixed many bugs in file, io, logging and time.
Browse files Browse the repository at this point in the history
  • Loading branch information
ax-6 committed Aug 2, 2024
1 parent bd699cf commit fd32b65
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 29 deletions.
16 changes: 8 additions & 8 deletions aqvm/base/file/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ int AqvmBaseFile_LockStream(struct AqvmBaseFile_File* stream) {
return -1;
}
if (stream->file == stdin) {
if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_inputMutex) != 0) {
if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_inputMutex) != 0) {
// TODO
return -2;
}
}else if (stream->file == stdout || stream->file == stderr) {
} else if (stream->file == stdout || stream->file == stderr) {
if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_outputMutex) != 0) {
// TODO
return -3;
Expand All @@ -60,7 +60,7 @@ int AqvmBaseFile_LockStream(struct AqvmBaseFile_File* stream) {
// TODO
return -4;
}
if (AqvmBaseFile_LockStream(stream) != 0) {
if (AqvmBaseFile_LockFile(stream) != 0) {
// TODO
return -5;
}
Expand All @@ -75,21 +75,21 @@ int AqvmBaseFile_UnlockStream(struct AqvmBaseFile_File* stream) {
return -1;
}
if (stream->file == stdin) {
if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_inputMutex) != 0) {
if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_inputMutex) != 0) {
// TODO
return -2;
}
} else if (stream->file == stdout || stream->file == stderr) {
if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_outputMutex) != 0) {
} else if (stream->file == stdout || stream->file == stderr) {
if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_outputMutex) != 0) {
// TODO
return -3;
}
} else {
if (AqvmBaseProcessFileLock_LockFile(stream) != 0) {
if (AqvmBaseProcessFileLock_UnlockFile(stream) != 0) {
// TODO
return -4;
}
if (AqvmBaseFile_LockStream(stream) != 0) {
if (AqvmBaseFile_UnlockFile(stream) != 0) {
// TODO
return -5;
}
Expand Down
21 changes: 15 additions & 6 deletions aqvm/base/io/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
#include "aqvm/base/file/file.h"

AqvmBaseThreadingMutex_Mutex AqvmBaseIo_inputMutex;
AqvmBaseThreadingMutex_Mutex
AqvmBaseIo_outputMutex;
AqvmBaseThreadingMutex_Mutex AqvmBaseIo_outputMutex;

struct AqvmBaseFile_File AqvmBaseIo_stdoutStream;
struct AqvmBaseFile_File* AqvmBaseIo_stdout = &AqvmBaseIo_stdoutStream;
Expand Down Expand Up @@ -75,25 +74,35 @@ int AqvmBaseIo_OutputLog(struct AqvmBaseFile_File* stream, const char* time,
}

char* name = va_arg(system_info, char*);
char* value = va_arg(system_info, char*);
char* value = NULL;
if (name != NULL) {
value = va_arg(system_info, char*);
}
while (name != NULL && value != NULL) {
if (fprintf(stream->file, ",\"%s\":\"%s\"", name, value) != 0) {
// TODO
return -4;
}
name = va_arg(system_info, char*);
value = va_arg(system_info, char*);
if (name != NULL) {
value = va_arg(system_info, char*);
}
}

name = va_arg(other_info, char*);
value = va_arg(other_info, char*);
value = NULL;
if (name != NULL) {
value = va_arg(system_info, char*);
}
while (name != NULL && value != NULL) {
if (fprintf(stream->file, ",\"%s\":\"%s\"", name, value) != 0) {
// TODO
return -5;
}
name = va_arg(other_info, char*);
value = va_arg(other_info, char*);
if (name != NULL) {
value = va_arg(system_info, char*);
}
}

if (fprintf(stream->file, "}\n") < 0) {
Expand Down
20 changes: 16 additions & 4 deletions aqvm/base/logging/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,22 @@ void AqvmBaseLogging_ProcessLog(const char* time, const char* type,

va_list system_info;
va_start(system_info, other_info);
AqvmBaseLogging_OutputLogToConsole(time, type, code, message, other_info,
system_info);
AqvmBaseLogging_OutputLogToFile(time, type, code, message, other_info,
system_info);

va_list system_info_to_console;
va_list other_info_to_console;
va_copy(system_info_to_console, system_info);
va_copy(other_info_to_console, other_info);

va_list system_info_to_file;
va_list other_info_to_file;
va_copy(system_info_to_file, system_info);
va_copy(other_info_to_file, other_info);

AqvmBaseLogging_OutputLogToConsole(
time, type, code, message, other_info_to_console, system_info_to_console);
AqvmBaseLogging_OutputLogToFile(time, type, code, message, other_info_to_file,
system_info_to_file);

va_end(system_info);
}

Expand Down
24 changes: 18 additions & 6 deletions aqvm/base/process/file_lock/unix/file_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,59 @@

#include <fcntl.h>
#include <stdio.h>
#include <errno.h>

#include "aqvm/base/file/file.h"

int AqvmBaseProcessFileLockUnix_LockFile(struct AqvmBaseFile_File* file) {
if (file == NULL || file->file == NULL) {
if (AqvmBaseFile_CheckStream(file) != 0) {
// TODO
return -1;
}
if (file->file == stdin || file->file == stdout || file->file == stderr) {
// TODO
return -2;
}

int file_descriptor = fileno(file->file);
if (file_descriptor == -1) {
return -2;
return -3;
}

struct flock file_lock = {0};
file_lock.l_type = F_WRLCK;
file_lock.l_whence = SEEK_SET;



if (fcntl(file_descriptor, F_SETLK, &file_lock) == -1) {
return -3;
printf("%i",errno);
return -4;
}
return 0;
}

int AqvmBaseProcessFileLockUnix_UnlockFile(struct AqvmBaseFile_File* file) {
if (file == NULL || file->file == NULL) {
if (AqvmBaseFile_CheckStream(file) != 0) {
// TODO
return -1;
}
if (file->file == stdin || file->file == stdout || file->file == stderr) {
// TODO
return -2;
}

int file_descriptor = fileno(file->file);
if (file_descriptor == -1) {
return -2;
return -3;
}

struct flock file_lock = {0};
file_lock.l_type = F_UNLCK;

if (fcntl(file_descriptor, F_SETLK, &file_lock) == -1) {
// TODO
return -3;
return -4;
}
return 0;
}
Expand Down
8 changes: 4 additions & 4 deletions aqvm/base/time/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

int AqvmBaseTime_localtime(const time_t timestamp, struct tm* result) {
#ifdef __unix__
if (AqvmBaseTimeUnix_localtime(timestamp, result)) {
if (AqvmBaseTimeUnix_localtime(timestamp, result) != 0) {
// TODO
return -1;
}
#elif _WIN32
if (AqvmBaseTimeWindows_localtime(timestamp, result)) {
if (AqvmBaseTimeWindows_localtime(timestamp, result) != 0) {
// TODO
return -2;
}
Expand All @@ -41,13 +41,13 @@ int AqvmBaseTime_localtime(const time_t timestamp, struct tm* result) {
}

int AqvmBaseTime_GetCurrentTimeString(char* result) {
if(result == NULL){
if (result == NULL) {
// TODO
return -1;
}

struct tm local_time;
if(AqvmBaseTime_localtime(time(NULL), &local_time)){
if (AqvmBaseTime_localtime(time(NULL), &local_time)) {
// TODO
return -2;
}
Expand Down
2 changes: 1 addition & 1 deletion aqvm/base/time/unix/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "aqvm/base/time/time.h"

int AqvmBaseTimeUnix_localtime(const time_t timestamp, struct tm* result) {
if (localtime_r(&timestamp, result) != 0) {
if (localtime_r(&timestamp, result) == NULL) {
// TODO
return -1;
}
Expand Down

0 comments on commit fd32b65

Please sign in to comment.