Skip to content

Instantly share code, notes, and snippets.

@igricart
Created July 29, 2020 14:46
Show Gist options
  • Save igricart/d36bcf268bdf26b637f580c6e8655802 to your computer and use it in GitHub Desktop.
Save igricart/d36bcf268bdf26b637f580c6e8655802 to your computer and use it in GitHub Desktop.
Script to handle interruption signal in c++ while also closing a log (Although it is unnecessary)
#include <iostream>
#include <syslog.h>
#include <string>
#include <csignal>
void signal_handler(int signal_num)
{
std::cout << "The interrupt signal is (" << signal_num << "). \n";
// terminate program
closelog();
std::cout << " Closed log " << std::endl;
exit(signal_num);
}
int main()
{
openlog("Logs", LOG_PID, LOG_USER);
std::string msg;
int i = 0;
//openlog("ConnectivityManager", LOG_PID, LOG_USER);
signal(SIGINT, signal_handler);
while (true)
{
syslog(LOG_INFO, "Start logging");
syslog(LOG_DEBUG, "oh yeah");
++i;
if (i == 10)
{
//raise(SIGINT);
}
}
//closelog();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment