-
-
Save binh12A3/095caa9d444e01a633ab16651195d92b to your computer and use it in GitHub Desktop.
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 "helper.h" | |
string my_pad_right(string const& str, int length, char c) | |
{ | |
if ( str.size() < length ) | |
return str + string(length - str.size(), c); | |
else | |
return str; | |
} | |
string my_pad_left(string const& str, int length, char c) | |
{ | |
if ( str.size() < length ) | |
return std::string(length - str.size(), c) + str; | |
else | |
return str; | |
} | |
// Cross-platform (Windows, Unix) sleep function | |
void my_sleep(int milliseconds) | |
{ | |
#ifdef _WIN32 | |
Sleep(milliseconds); | |
#else | |
usleep(milliseconds * 1000); | |
#endif | |
} | |
void my_log(string str, string log_level) | |
{ | |
time_t tmNow; | |
tmNow = time(NULL); | |
struct tm t = *localtime(&tmNow); | |
string date_time = my_pad_left(to_string(t.tm_mday ), 2, '0') + "/" | |
+ my_pad_left(to_string(t.tm_mon + 1), 2, '0') + "/" | |
+ my_pad_left(to_string(t.tm_year + 1900), 4, '0') + " " | |
+ my_pad_left(to_string(t.tm_hour ), 2, '0') + ":" | |
+ my_pad_left(to_string(t.tm_min ), 2, '0') + ":" | |
+ my_pad_left(to_string(t.tm_sec ), 2, '0'); | |
//printf("%s", date_time.c_str()); //For printf you need a c-style string | |
int pid, ppid; | |
#ifdef _WIN32 | |
pid = ::GetCurrentProcessId(); | |
#else | |
pid = ::getpid(); | |
ppid = ::getppid(); | |
#endif | |
cout << "[" << date_time << "][PID:" << pid << "][PPID:" << ppid << "]" << log_level << " " << str << endl; | |
} |
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
// This is a protection to avoid cross duplidated when many sources include this .h file | |
#ifndef _HELPER_ | |
#define _HELPER_ | |
#include <iostream> // for using std | |
#include <string> // for using std::string | |
#include <ctime> // for using time | |
#ifdef _WIN32 | |
#include <windows.h> // for using Sleep() on Windows | |
#else | |
#include <unistd.h> // for using usleep() on Unix | |
#endif // _WIN32 | |
using std::cout; | |
using std::endl; | |
using std::string; | |
using std::to_string; | |
#define LL_FATAL "[FATAL]" | |
#define LL_ERROR "[ERROR]" | |
#define LL_WARNING "[WARN ]" | |
#define LL_INFO "[INFO ]" | |
#define LL_DEBUG "[DEBUG]" | |
#define LL_TRACE "[TRACE]" | |
void my_log(const string str, string log_level = LL_INFO); | |
void my_sleep(const int milliseconds); | |
string my_pad_right(string const& str, const int length, const char c); | |
string my_pad_left(string const& str, const int length, const char c); | |
#endif // _HELPER_ |
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
/* | |
COMPILE : gcc test.cpp mylib/helper.cpp -lstdc++ -o test | |
*/ | |
#include <iostream> // for using std, printf(),... | |
#include <string> // for using std::string | |
#include <cstring> // for using strcpy() | |
#include <unistd.h> // for using fork() | |
#include "mylib/helper.h" | |
using std::cout; | |
using std::endl; | |
using std::string; | |
using std::to_string; | |
int main(int argc, char** argv) { | |
my_log("Hello world"); | |
int pid; | |
pid = fork(); | |
if(pid != 0) | |
{ | |
while(1) | |
{ | |
my_log("Parent process"); | |
my_sleep(1000); | |
} | |
} | |
else | |
{ | |
while(1) | |
{ | |
my_log("Child process", LL_DEBUG); | |
my_sleep(1000); | |
// copy a into NULL pointer to simulate a coredump in order to produce a Zombie process | |
strcpy("a", NULL); | |
} | |
} | |
return 0; | |
} |
Author
binh12A3
commented
Sep 3, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment