Last active
December 19, 2017 04:15
-
-
Save slwu89/5c1ebb0290d4e28d685987a90e98bcc9 to your computer and use it in GitHub Desktop.
stupid simple logging; not thread safe, havent deleted copy assignment/constructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
#include <string> | |
#include <iostream> | |
/* singleton for logging */ | |
class logger { | |
private: | |
std::ofstream output; | |
static logger* l_instance; | |
logger(){ | |
std::cout << "logger being born at " << this << std::endl; | |
}; | |
~logger(){ | |
std::cout << "logger being killed at " << this << std::endl; | |
}; | |
public: | |
void log(const std::string& out){ | |
if(output.is_open()==0){ | |
output.open("/Users/slwu89/Desktop/log.txt"); | |
output << out << std::endl; | |
} else { | |
output << out << std::endl; | |
} | |
}; | |
void suicide(){ | |
std::cout << "logger suiciding at " << this << std::endl; | |
logger::~logger(); | |
}; | |
static logger* instance(){ | |
if(!l_instance){ | |
l_instance = new logger; | |
}; | |
return l_instance; | |
}; | |
}; | |
logger *logger::l_instance = nullptr; | |
/* test human */ | |
class human { | |
public: | |
human(const std::string& _name) : name(_name) { | |
std::cout << name << " being born at " << this << std::endl; | |
}; | |
~human(){ | |
std::string death = name + " is dying"; | |
logger::instance()->log(death); | |
std::cout << name << " being killed at " << this << std::endl; | |
}; | |
void log_name(){ | |
logger::instance()->log(name); | |
}; | |
std::string& get_name(){ return name; }; | |
private: | |
std::string name; | |
}; | |
int main() { | |
human* bob = new human("bob"); | |
human* alice = new human("alice"); | |
bob->log_name(); | |
alice->log_name(); | |
bob->log_name(); | |
alice->log_name(); | |
delete bob; | |
delete alice; | |
logger::instance()->suicide(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment