Skip to content

Commit

Permalink
move uid and log to source files
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Eckels committed Jun 3, 2020
1 parent 0f47489 commit 2a65c07
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 60 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ install(FILES ${POLYHOOK_CORE_HEADERS} DESTINATION include/polyhook2)
target_sources(${PROJECT_NAME} PRIVATE
${PROJECT_SOURCE_DIR}/sources/MemProtector.cpp
${PROJECT_SOURCE_DIR}/sources/TestEffectTracker.cpp
${PROJECT_SOURCE_DIR}/sources/PageAllocator.cpp)
${PROJECT_SOURCE_DIR}/sources/PageAllocator.cpp
${PROJECT_SOURCE_DIR}/sources/ErrorLog.cpp
${PROJECT_SOURCE_DIR}/sources/UID.cpp)

#DisAsm/Capstone
if(POLYHOOK_DISASM_CAPSTONE)
Expand Down
48 changes: 5 additions & 43 deletions polyhook2/ErrorLog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,15 @@ struct Error {

class ErrorLog {
public:
void setLogLevel(ErrorLevel level) {
m_logLevel = level;
}

void push(std::string msg, ErrorLevel level) {
push({ std::move(msg), level });
}

void push(Error err) {
if (err.lvl >= m_logLevel) {
switch (err.lvl) {
case ErrorLevel::INFO:
std::cout << "[+] Info: " << err.msg << std::endl;
break;
case ErrorLevel::WARN:
std::cout << "[!] Warn: " << err.msg << std::endl;
break;
case ErrorLevel::SEV:
std::cout << "[!] Error: " << err.msg << std::endl;
break;
default:
std::cout << "Unsupported error message logged " << err.msg << std::endl;
}
}

m_log.push_back(std::move(err));
}

Error pop() {
Error err{};
if (!m_log.empty()) {
err = std::move(m_log.back());
m_log.pop_back();
}
return err;
}

static ErrorLog& singleton() {
static ErrorLog log;
return log;
}
void setLogLevel(ErrorLevel level);
void push(std::string msg, ErrorLevel level);
void push(Error err);
Error pop();
static ErrorLog& singleton();
private:
std::vector<Error> m_log;
ErrorLevel m_logLevel = ErrorLevel::INFO;
};


}

#endif
2 changes: 1 addition & 1 deletion polyhook2/Tests/TestEffectTracker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Effect {
bool didExecute();
private:
bool m_executed;
UID m_uid;
PLH::UID m_uid;
};

/**Track if some side effect happened.**/
Expand Down
22 changes: 8 additions & 14 deletions polyhook2/UID.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@
#define POLYHOOK_2_UID_HPP

#include <atomic>
namespace PLH {
class UID {
public:
UID(long val);
static std::atomic_long& singleton();

class UID {
public:
UID(long val) {
this->val = val;
}

static std::atomic_long& singleton() {
static std::atomic_long base = {-1};
base++;
return base;
}

long val;
};
long val;
};
}
#endif //POLYHOOK_2_UID_HPP
43 changes: 43 additions & 0 deletions sources/ErrorLog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "polyhook2/ErrorLog.hpp"

void PLH::ErrorLog::setLogLevel(PLH::ErrorLevel level) {
m_logLevel = level;
}

void PLH::ErrorLog::push(std::string msg, PLH::ErrorLevel level) {
push({ std::move(msg), level });
}

void PLH::ErrorLog::push(PLH::Error err) {
if (err.lvl >= m_logLevel) {
switch (err.lvl) {
case ErrorLevel::INFO:
std::cout << "[+] Info: " << err.msg << std::endl;
break;
case ErrorLevel::WARN:
std::cout << "[!] Warn: " << err.msg << std::endl;
break;
case ErrorLevel::SEV:
std::cout << "[!] Error: " << err.msg << std::endl;
break;
default:
std::cout << "Unsupported error message logged " << err.msg << std::endl;
}
}

m_log.push_back(std::move(err));
}

PLH::Error PLH::ErrorLog::pop() {
Error err{};
if (!m_log.empty()) {
err = std::move(m_log.back());
m_log.pop_back();
}
return err;
}

PLH::ErrorLog& PLH::ErrorLog::singleton() {
static ErrorLog log;
return log;
}
2 changes: 1 addition & 1 deletion sources/TestEffectTracker.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "polyhook2/Tests/TestEffectTracker.hpp"


Effect::Effect() : m_uid(UID::singleton()) {
Effect::Effect() : m_uid(PLH::UID::singleton()) {
m_executed = false;
}

Expand Down
11 changes: 11 additions & 0 deletions sources/UID.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "polyhook2/UID.hpp"

PLH::UID::UID(long val) {
this->val = val;
}

std::atomic_long& PLH::UID::singleton() {
static std::atomic_long base = { -1 };
base++;
return base;
}

0 comments on commit 2a65c07

Please sign in to comment.