Skip to content

Commit

Permalink
Temporarily saved, refactoring logging, incomplete.
Browse files Browse the repository at this point in the history
  • Loading branch information
ax-6 committed Jul 29, 2024
1 parent e0c2969 commit 47de6c5
Show file tree
Hide file tree
Showing 17 changed files with 194 additions and 120 deletions.
16 changes: 8 additions & 8 deletions aq/aq.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@

int main(int argc, char *argv[]) {
// TODO(Aqvm): Finish this function after completing AQVM development.
AqvmBaseLogging_OutputLog("\"INFO\"", "\"main_Start\"",
"\"Aq main program has been started.\"", NULL);
AqvmBaseLogging_OutputLog("INFO", "main_Start",
"Aq main program has been started.", NULL);

// TODO(Aq): Before the official release, remove the judgment logic for
// command line arguments and design a dedicated component to parse command
// line arguments.
if (argc < 2) {
AqvmBaseLogging_OutputLog("\"ERROR\"", "\"main_ArgsError\"",
"\"Please provide a file name as an argument.\"",
AqvmBaseLogging_OutputLog("ERROR", "main_ArgsError",
"Please provide a file name as an argument.",
NULL);
return -1;
}

if (Aqvm_StartVm(argv[1]) != 0) {
AqvmBaseLogging_OutputLog("\"ERROR\"", "\"main_InitVmError\"",
"\"Starting Aqvm met error.\"", NULL);
AqvmBaseLogging_OutputLog("ERROR", "main_InitVmError",
"Starting Aqvm met error.", NULL);
return -1;
}

AqvmBaseLogging_OutputLog("\"INFO\"", "\"main_End\"",
"\"Aq main program has ended.\"", NULL);
AqvmBaseLogging_OutputLog("INFO", "main_End", "Aq main program has ended.",
NULL);
return 0;
}
2 changes: 1 addition & 1 deletion aq/aq.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

#include "aqvm/aqvm.h"

int main(int argc, char *argv[]);
int main(int argc, char* argv[]);

#endif
4 changes: 3 additions & 1 deletion aqvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/aqvm.c)

add_subdirectory(base)
add_subdirectory(memory)
add_subdirectory(bytecode)
add_subdirectory(interpreter)

add_library(Aqvm STATIC ${SOURCES})

target_link_libraries(Aqvm PRIVATE AqvmMemory)
target_link_libraries(Aqvm PRIVATE AqvmBase)
target_link_libraries(Aqvm PRIVATE AqvmMemory)
target_link_libraries(Aqvm PRIVATE AqvmBytecode)
target_link_libraries(Aqvm PRIVATE AqvmInterpreter)
10 changes: 5 additions & 5 deletions aqvm/aqvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

int Aqvm_StartVm(const char* FileName) {
// TODO(Aqvm): Finish this function after completing AQVM development.
AqvmBaseLogging_OutputLog("\"INFO\"", "\"Aqvm_StartVm_Start\"",
"\"Initializing Aqvm has been started.\"", NULL);
AqvmBaseLogging_OutputLog("INFO", "Aqvm_StartVm_Start",
"Initializing Aqvm has been started.", NULL);

if (AqvmMemory_CheckMemoryConditions() != 0) {
AqvmBaseLogging_OutputLog(
"\"ERROR\"", "\"Aqvm_StartVm_CheckMemoryConditionsError\"",
"\"Checking memory conditions met error.\"", NULL);
AqvmBaseLogging_OutputLog("ERROR",
"Aqvm_StartVm_CheckMemoryConditionsError",
"Checking memory conditions met error.", NULL);
return -1;
}

Expand Down
102 changes: 88 additions & 14 deletions aqvm/base/logging/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

void AqvmBaseLogging_OutputLog(const char* type, const char* code,
const char* message, const char* other_info) {
if (type == NULL) {
/*if (type == NULL) {
type = "NULL";
}
if (code == NULL) {
Expand All @@ -23,30 +23,104 @@ void AqvmBaseLogging_OutputLog(const char* type, const char* code,
}
if (other_info == NULL) {
other_info = "NULL";
}
}*/

char time_str[28];
time_t current_time = time(NULL);
strftime(time_str, 28, "\"%Y-%m-%dT%H:%M:%S%z\"", localtime(&current_time));
strftime(time_str, 28, "%Y-%m-%dT%H:%M:%S%z", localtime(&current_time));

fprintf(stderr,
"{\"Time\":%s,\"Type\":%s,\"Code\":%s,\"Message\":%s,\"ErrnoInfo\":{"
"\"Errno\":%d,\"Message\":\"%s\"},\"OtherInfo\":%s}\n",
AqvmBaseLogging_ProcessLog(time_str, type, code, message, errno,
strerror(errno), other_info);

/*fprintf(stderr,
"{Time:%s,Type:%s,Code:%s,Message:%s,ErrnoInfo:{Errno:%d,Message:%s},"
"OtherInfo:%s}\n",
time_str, type, code, message, errno, strerror(errno), other_info);
FILE* log_ptr = fopen(".aqvm_log.log", "a");
if (log_ptr == NULL) {
fprintf(
stderr,
"{\"Time\":%s,\"Type\":%s,\"Code\":%s,\"Message\":%s,\"ErrnoInfo\":{"
"\"Errno\":%d,\"Message\":\"%s\"},\"OtherInfo\":%s}\n",
time_str, "ERROR", "AqvmBaseLogging_OutputLog_OutputToFileError",
"Failed to open log file", errno, strerror(errno), "NULL");
fprintf(stderr,
"{Time:%s,Type:%s,Code:%s,Message:%s,ErrnoInfo:{Errno:%d,Message:%"
"s},OtherInfo:%s}\n",
time_str, "ERROR", "AqvmBaseLogging_OutputLog_OutputToFileError",
"Failed to open log file", errno, strerror(errno), "NULL");
return;
}
fprintf(log_ptr,
"{\"Time\":%s,\"Type\":%s,\"Code\":%s,\"Message\":%s,\"ErrnoInfo\":{"
"\"Errno\":%d,\"Message\":\"%s\"},\"OtherInfo\":%s}\n",
"{Time:%s,Type:%s,Code:%s,Message:%s,ErrnoInfo:{"
"Errno:%d,Message:%s},OtherInfo:%s}\n",
time_str, type, code, message, errno, strerror(errno), other_info);
fclose(log_ptr);*/
}

void AqvmBaseLogging_ProcessLog(const char* time, const char* type,
const char* code, const char* message,
int error_number, const char* errno_message,
const char* other_info) {
if (time == NULL) {
time = "NULL";
}
if (type == NULL) {
type = "NULL";
}
if (code == NULL) {
code = "NULL";
}
if (message == NULL) {
message = "NULL";
}
if (errno_message == NULL) {
errno_message = "NULL";
}
if (other_info == NULL) {
other_info = "NULL";
}

AqvmBaseLogging_OutputLogToConsole(time, type, code, message, error_number,
errno_message, other_info);
AqvmBaseLogging_OutputLogToFile(time, type, code, message, error_number,
errno_message, other_info);
}

int AqvmBaseLogging_OutputLogToConsole(const char* time, const char* type,
const char* code, const char* message,
int error_number,
const char* errno_message,
const char* other_info) {
fprintf(stderr,
"{\"Time\":\"%s\",\"Type\":\"%s\",\"Code\":\"%s\",\"Message\":\"%s\","
"\"ErrnoInfo\":{\"Errno\":%d,\"Message\":\"%s\"},"
"\"OtherInfo\":\"%s\"}\n",
time, type, code, message, error_number, errno_message, other_info);
}

/*int AqvmBaseLogging_OutputLogToConsole(const char* format, ...) {
va_list args;
va_start(args, format);
int result = vfprintf(stderr, format, args);
va_end(args);
return result;
}*/

int AqvmBaseLogging_OutputLogToFile(const char* time, const char* type,
const char* code, const char* message,
int error_number, const char* errno_message,
const char* other_info) {
FILE* log_ptr = fopen(".aqvm_log.log", "a");
if (log_ptr == NULL) {
fprintf(stderr,
"{Time:%s,Type:%s,Code:%s,Message:%s,ErrnoInfo:{Errno:%d,Message:%"
"s},OtherInfo:%s}\n",
time, "ERROR", "AqvmBaseLogging_OutputLog_OutputToFileError",
"Failed to open log file", errno, strerror(errno), "NULL");
return;
}
fprintf(log_ptr,
"{\"Time\":\"%s\",\"Type\":\"%s\",\"Code\":\"%s\",\"Message\":\"%s\","
"\"ErrnoInfo\":{\"Errno\":%d,\"Message\":\"%s\"},"
"\"OtherInfo\":\"%s\"}\n",
time, type, code, message, error_number, errno_message, other_info);
fclose(log_ptr);
}
19 changes: 17 additions & 2 deletions aqvm/base/logging/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,24 @@
// should be an additional information to the current log (e.g. system
// information).
// NOTICE: If you need to use the function, please use json format. The output
// is json format. For example, AqvmBaseLogging_OutputLog("\"type\"",
// "\"code\"", "\"message\"", "\"other_info\"");
// is json format. For example, AqvmBaseLogging_OutputLog("type",
// "code", "message", "other_info");
void AqvmBaseLogging_OutputLog(const char* type, const char* code,
const char* message, const char* other_info);

void AqvmBaseLogging_ProcessLog(const char* time, const char* type,
const char* code, const char* message, int error_number,
const char* errno_message,
const char* other_info);

int AqvmBaseLogging_OutputLogToConsole(const char* time, const char* type,
const char* code, const char* message, int error_number,
const char* errno_message,
const char* other_info);

int AqvmBaseLogging_OutputLogToFile(const char* time, const char* type,
const char* code, const char* message, int error_number,
const char* errno_message,
const char* other_info);

#endif
14 changes: 10 additions & 4 deletions aqvm/base/time/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ include_directories(${PROJECT_SOURCE_DIR})

set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/time.c)

add_subdirectory(unix)
add_subdirectory(windows)
if (UNIX)
add_subdirectory(unix)
elseif (WIN32)
add_subdirectory(windows)
endif()

add_library(AqvmBaseTime STATIC ${SOURCES})

target_link_libraries(AqvmBaseTime PRIVATE AqvmBaseTimeUnix)
target_link_libraries(AqvmBaseTime PRIVATE AqvmBaseTimeWindows)
if (UNIX)
target_link_libraries(AqvmBaseTime PRIVATE AqvmBaseTimeUnix)
elseif (WIN32)
target_link_libraries(AqvmBaseTime PRIVATE AqvmBaseTimeWindows)
endif()
4 changes: 2 additions & 2 deletions aqvm/base/time/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
int AqvmBaseTime_localtime(const time_t timestamp, struct tm* result) {
// TODO
AqvmBaseLogging_OutputLog(
"\"WARNING\"", "\"AqvmBaseTime_localtime_ThreadUnsafeWarning\"",
"WARNING", "AqvmBaseTime_localtime_ThreadUnsafeWarning",
"The localtime function may cause thread unsafety.", NULL);
struct tm* local_time = localtime(&timestamp);
if (local_time == NULL) {
Expand All @@ -23,6 +23,6 @@ int AqvmBaseTime_localtime(const time_t timestamp, struct tm* result) {

int AqvmBaseTime_GetCurrentTimeString(char* result) {
time_t current_time = time(NULL);
strftime(result, 28, "\"%Y-%m-%dT%H:%M:%S%z\"", localtime(&current_time));
strftime(result, 28, "%Y-%m-%dT%H:%M:%S%z", localtime(&current_time));
return 0;
}
3 changes: 2 additions & 1 deletion aqvm/base/time/unix/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// This program is licensed under the AQ License. You can find the AQ license in
// the root directory.

#include "aqvm/base/time/time.h"
#include "aqvm/base/time/unix/time.h"

#include <time.h>

#include "aqvm/base/time/time.h"
#include "aqvm/base/logging/logging.h"

int AqvmBaseTime_localtime(const time_t timestamp, struct tm* result) {
Expand Down
17 changes: 6 additions & 11 deletions aqvm/base/time/windows/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,17 @@
// This program is licensed under the AQ License. You can find the AQ license in
// the root directory.

#include "aqvm/base/time/time.h"
#include "aqvm/base/time/windows/time.h"

#include <time.h>

#include "aqvm/base/time/time.h"
#include "aqvm/base/logging/logging.h"

int AqvmBaseTime_localtime(const time_t timestamp, struct tm* result) {
// TODO
AqvmBaseLogging_OutputLog(
"\"WARNING\"", "\"AqvmBaseTime_localtime_ThreadUnsafeWarning\"",
"The localtime function may cause thread unsafety.", NULL);
struct tm* local_time = localtime(&timestamp);
if (local_time == NULL) {
return -2;
}
*result = *local_time;
int AqvmBaseTimeWindows_localtime(const time_t timestamp, struct tm* result) {
if (localtime_s(result, &timestamp) != 0) {AqvmBaseLogging_OutputLog("AqvmBaseTimeWindows_localtime_","","",NULL);return -1;
}
return 0;
return 0;
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ include_directories(${PROJECT_SOURCE_DIR})

set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/bytecode.c)

add_library(AqvmInterpreterBytecode STATIC ${SOURCES})
add_library(AqvmBytecode STATIC ${SOURCES})

target_link_libraries(AqvmInterpreterBytecode PRIVATE AqvmBase)
target_link_libraries(AqvmBytecode PRIVATE AqvmBase)
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions aqvm/interpreter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/interpreter.c)

add_library(AqvmInterpreter STATIC ${SOURCES})

add_subdirectory(bytecode)
add_subdirectory(operator)

target_link_libraries(AqvmInterpreter PRIVATE AqvmBase)
target_link_libraries(AqvmInterpreter PRIVATE AqvmMemory)
target_link_libraries(AqvmInterpreter PRIVATE AqvmInterpreterBytecode)
target_link_libraries(AqvmInterpreter PRIVATE AqvmBytecode)
target_link_libraries(AqvmInterpreter PRIVATE AqvmInterpreterOperator)
2 changes: 1 addition & 1 deletion aqvm/interpreter/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
#ifndef AQ_AQVM_INTERPRETER_INTERPRETER_H_
#define AQ_AQVM_INTERPRETER_INTERPRETER_H_

#include "aqvm/interpreter/bytecode/bytecode.h"
#include "aqvm/bytecode/bytecode.h"

#endif
6 changes: 2 additions & 4 deletions aqvm/interpreter/operator/operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ int AqvmInterpreterOperator_SAR(
struct AqvmMemory_Memory* operand1,
struct AqvmMemory_Memory* operand2);
int AqvmInterpreterOperator_IF();
int AqvmInterpreterOperator_AND(bool result, bool operand1,
bool operand2);
int AqvmInterpreterOperator_AND(bool result, bool operand1, bool operand2);
int AqvmInterpreterOperator_OR(bool result, bool operand1, bool operand2);
int AqvmInterpreterOperator_XOR(bool result, bool operand1,
bool operand2);
int AqvmInterpreterOperator_XOR(bool result, bool operand1, bool operand2);
int AqvmInterpreterOperator_CMP(
bool result, int opcode, struct AqvmMemory_Memory * operand1,
struct AqvmMemory_Memory* operand2);
Expand Down
Loading

0 comments on commit 47de6c5

Please sign in to comment.