From 3d8bf74fbf2dd042d03ba3d993a7759e2fbb21d9 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Mon, 4 Nov 2019 23:17:15 +0100 Subject: [PATCH 1/3] + getSharedMemory --- src/internal.cpp | 24 +++++++++++++++++++++++- src/internal.h | 15 +++++++++++---- src/vkvm.cpp | 4 +++- 3 files changed, 37 insertions(+), 6 deletions(-) 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/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; } From 53afcffba81afaf2c535dd72d612f7230d3ef150 Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 5 Nov 2019 15:08:17 +0100 Subject: [PATCH 2/3] + logging --- src/log.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/log.h | 19 +++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/log.cpp create mode 100644 src/log.h diff --git a/src/log.cpp b/src/log.cpp new file mode 100644 index 0000000..2dd85ff --- /dev/null +++ b/src/log.cpp @@ -0,0 +1,60 @@ + +#include "log.h" +#include + +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"; + } +} + +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; + +void log(const std::string &msg, LogLevel level) { + if(level >= logLevel) { + std::string levelName = getLevelName(level); + int maxLevelNameLength = 8; + + std::cout << "["; + //color and level name;lo + std::cout << "\033[" << getLevelColor(level) << "m" << levelName << "\033[0m"; + std::cout << "] "; + for (int i = levelName.size(); i < maxLevelNameLength; i++) { + std::cout << " "; + } + std::cout << msg << "\n"; + } +} + +void setLogLevel(LogLevel level) { + logLevel = level; +} + diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..b7248b1 --- /dev/null +++ b/src/log.h @@ -0,0 +1,19 @@ +#ifndef LIBRARY_LOG_H +#define LIBRARY_LOG_H + +#include + +enum LogLevel{ + DEBUG = 1, + INFO = 2, + WARNING = 3, + ERROR = 4, + CRITICAL = 5, + OFF = 6 +}; + +void log(const std::string &msg, LogLevel level = LogLevel::INFO); + +void setLogLevel(LogLevel level); + +#endif //LIBRARY_LOG_H From 9b89e46de26a2dbe14018f90ed56c1b7232b8e9c Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 5 Nov 2019 15:19:51 +0100 Subject: [PATCH 3/3] + timestamps for logging --- src/log.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/log.h | 11 +++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/log.cpp b/src/log.cpp index 2dd85ff..42dfbe5 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -1,7 +1,13 @@ +/** + * @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: @@ -19,6 +25,7 @@ const char *getLevelName(LogLevel level){ } } +//converts the level to a ansi color code const char *getLevelColor(LogLevel level){ switch(level){ case LogLevel::DEBUG: @@ -38,19 +45,61 @@ const char *getLevelColor(LogLevel level){ 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 << " "; } - std::cout << msg << "\n"; + + //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"; } } diff --git a/src/log.h b/src/log.h index b7248b1..6b7f94f 100644 --- a/src/log.h +++ b/src/log.h @@ -12,8 +12,19 @@ enum LogLevel{ 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