diff --git a/src/internal.cpp b/src/internal.cpp index d9575d2..2fefc58 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -1,5 +1,27 @@ #include "internal.h" +#include +Impl impl; -Internal internal; +void *getSharedMemory(){ + impl.sharedMemorySize = 8000; + auto id = shmget(impl.sharedMemoryKey, impl.sharedMemorySize, 0644 | IPC_CREAT); + if(id == -1){ + //error + impl.sharedMemorySize = 0; + return nullptr; + } + void *data = shmat(id, nullptr, 0); + if(data == (char*)(-1)){ + //error + impl.sharedMemorySize = 0; + return nullptr; + } + + return data; +} + +int getSharedMemorySize(){ + return impl.sharedMemorySize; +} diff --git a/src/internal.h b/src/internal.h index 65baee5..831ad27 100644 --- a/src/internal.h +++ b/src/internal.h @@ -6,13 +6,14 @@ #include "LayoutVersion.h" #include "GraphicMode.h" #include "Color.h" +#include /** * the Control Registers * @author Julian Hinxlage * @since 0.1.0 */ -struct Registers{ +struct Registers { int layout_version; int trigger_reset; int width_pixels; @@ -34,7 +35,7 @@ struct Registers{ int keyboardBuffer_index_r; }; -struct InterruptEntry{ +struct InterruptEntry { int pid; int signum; }; @@ -42,10 +43,16 @@ struct InterruptEntry{ /** * internal values for the library. */ -struct Internal{ +struct Impl { int sharedMemoryPid; + key_t sharedMemoryKey; + int sharedMemorySize; }; -extern Internal internal; +extern Impl impl; + +void *getSharedMemory(); + +int getSharedMemorySize(); /** * set layout version. diff --git a/src/log.cpp b/src/log.cpp new file mode 100644 index 0000000..42dfbe5 --- /dev/null +++ b/src/log.cpp @@ -0,0 +1,109 @@ + +/** + * @author Julian Hinxlage + * @since 0.1.0 + */ + +#include "log.h" +#include + +//converts the level to a string of the level +const char *getLevelName(LogLevel level){ + switch(level){ + case LogLevel::DEBUG: + return "DEBUG"; + case LogLevel::INFO: + return "INFO"; + case LogLevel::WARNING: + return "WARNING"; + case LogLevel::ERROR: + return "ERROR"; + case LogLevel::CRITICAL: + return "CRITICAL"; + default: + return "NON"; + } +} + +//converts the level to a ansi color code +const char *getLevelColor(LogLevel level){ + switch(level){ + case LogLevel::DEBUG: + return "0;37"; + case LogLevel::INFO: + return "0"; + case LogLevel::WARNING: + return "1;33"; + case LogLevel::ERROR: + return "1;31"; + case LogLevel::CRITICAL: + return "1;35"; + default: + return "0"; + } +} + +LogLevel logLevel = LogLevel::INFO; + +//log the current time +void logTime(){ + time_t rawtime; + time(&rawtime); + struct tm *timeinfo; + timeinfo = localtime(&rawtime); + + if (timeinfo->tm_hour < 10) { + std::cout << "0"; + } + std::cout << timeinfo->tm_hour; + std::cout << ":"; + if (timeinfo->tm_min < 10) { + std::cout << "0"; + } + std::cout << timeinfo->tm_min; + std::cout << ":"; + if (timeinfo->tm_sec < 10) { + std::cout << "0"; + } + std::cout << timeinfo->tm_sec; +} + +//log the message +void log(const std::string &msg, LogLevel level) { + if(level >= logLevel) { + std::string levelName = getLevelName(level); + int maxLevelNameLength = 8; + + //time + std::cout << "["; + logTime(); + std::cout << "] "; + + //color and level name;lo + std::cout << "["; + std::cout << "\033[" << getLevelColor(level) << "m" << levelName << "\033[0m"; + std::cout << "] "; + for (int i = levelName.size(); i < maxLevelNameLength; i++) { + std::cout << " "; + } + + //message + for(char c : msg){ + if(c == '\n'){ + //intend newlines so that they align with the start of the message + std::cout << "\n"; + for(int i = 0; i < 22;i++){ + std::cout << " "; + } + }else{ + std::cout << c; + } + } + std::cout << "\n"; + } +} + +void setLogLevel(LogLevel level) { + logLevel = level; +} + diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..6b7f94f --- /dev/null +++ b/src/log.h @@ -0,0 +1,30 @@ +#ifndef LIBRARY_LOG_H +#define LIBRARY_LOG_H + +#include + +enum LogLevel{ + DEBUG = 1, + INFO = 2, + WARNING = 3, + ERROR = 4, + CRITICAL = 5, + OFF = 6 +}; + +/** + * log the messgae with logLevel and timestamp + * @author Julian Hinxlage + * @since 0.1.0 + */ +void log(const std::string &msg, LogLevel level = LogLevel::INFO); + + +/** + * set the logLevel, the log function will use this to determine if the message is logged + * @author Julian Hinxlage + * @since 0.1.0 + */ +void setLogLevel(LogLevel level); + +#endif //LIBRARY_LOG_H diff --git a/src/vkvm.cpp b/src/vkvm.cpp index 45218e4..1b63d02 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -2,5 +2,7 @@ #include "internal.h" void initialize(int pid) { - internal.sharedMemoryPid = pid; + impl.sharedMemoryPid = pid; + impl.sharedMemoryKey = 892348; + impl.sharedMemorySize = 0; }