前言
写个发syslog的测试程序,用到无回显,不等待回车的取单个字符api getch.
但是linux中没这个函数.
查资料,stackoverflow上有人解决了。
写个测试程序试试,以后用到的时候在整理。
实验
还是将可复用的功能性代码放在单独的文件中好。
以后用到的时候再整理.
// @file main.cpp
// view date time
// date "+DATE: %m/%d/%y%nTIME: %H:%M:%S"
// set date time
// date -s "2018-3-13 14:25:00"
// https://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux
#include <stdlib.h>
#include <stdio.h>
// #include <string.h>
#include <string>
#include <termios.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <vector>
#include "my_syslog.h"
#include "ls_regex.h"
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long ulong;
typedef unsigned long long ull;
#define LINE80 "--------------------------------------------------------------------------------"
#ifndef SAFE_DELETE
#define SAFE_DELETE(p) \
if (NULL != (p)) { \
delete (p); \
(p) = NULL; \
}
#endif // #ifndef SAFE_DELETE
static struct termios old_tos;
static struct termios new_tos;
void init();
void uninit();
int fn_test(int argc, char** argv);
/* Initialize new terminal i/o settings */
void initTermios(int echo)
{
tcgetattr(0, &old_tos); /* grab old terminal i/o settings */
new_tos = old_tos; /* make new settings same as old settings */
new_tos.c_lflag &= ~ICANON; /* disable buffered i/o */
if (echo) {
new_tos.c_lflag |= ECHO; /* set echo mode */
} else {
new_tos.c_lflag &= ~ECHO; /* set no echo mode */
}
tcsetattr(0, TCSANOW, &new_tos); /* use these new terminal i/o settings now */
}
/* Restore old terminal i/o settings */
void resetTermios(void)
{
tcsetattr(0, TCSANOW, &old_tos);
}
/* Read 1 character - echo defines echo mode */
char getch_(int echo)
{
char ch;
initTermios(echo);
ch = getchar();
resetTermios();
return ch;
}
/* Read 1 character without echo */
char getch(void)
{
return getch_(0);
}
/* Read 1 character with echo */
char getche(void)
{
return getch_(1);
}
int main(int argc, char** argv)
{
system("clear");
init();
fn_test(argc, argv);
uninit();
return EXIT_SUCCESS;
}
void init()
{
ns_syslog::open_syslog("test_new");
ns_syslog::g_log_condition.b_EMERG = false;
ns_syslog::g_log_condition.b_CRIT = true;
ns_syslog::g_log_condition.b_ALERT = true;
ns_syslog::g_log_condition.b_ERR = true;
ns_syslog::g_log_condition.b_WARNING = true;
ns_syslog::g_log_condition.b_NOTICE = true;
ns_syslog::g_log_condition.b_INFO = true;
ns_syslog::g_log_condition.b_DEBUG = true;
ns_syslog::set_log_level(
ns_syslog::g_log_condition.b_EMERG,
ns_syslog::g_log_condition.b_ALERT,
ns_syslog::g_log_condition.b_CRIT,
ns_syslog::g_log_condition.b_ERR,
ns_syslog::g_log_condition.b_WARNING,
ns_syslog::g_log_condition.b_NOTICE,
ns_syslog::g_log_condition.b_INFO,
ns_syslog::g_log_condition.b_DEBUG);
}
void uninit()
{
ns_syslog::close_syslog();
}
int fn_test(int argc, char** argv)
{
char c_input = '\0';
long l_send_cnt = 0;
do {
printf("if need quit, press 'q', other key to send local syslog\n");
c_input = getch(); // 无回显,不等待回车
if ('q' == c_input) {
break;
}
MYLOG_D("%ld test send local syslog\n", l_send_cnt++);
} while (1);
return EXIT_SUCCESS;
}