linuxConsole/mLog.cpp
/*Write to logfile given line, with marked by idx*/
Write to logfile given line, with marked by idx
Write to logfile given line with marked by idx
int inLog(std::string line, int idx, bool showInConsole)
{
	int fd = openLogFile(3);
	if (fd == -1) { printf("Giveup to open log file %s\n", logFilePath); return fd; }
	if (flock(fd, 2) != 0) { printf("Giveup to lock log file %s\n", logFilePath); close(fd); return fd; }
	line.insert(0, getHeader(std::to_string(idx)));
	if (showInConsole) { printf("%s\n", line.c_str()); }
	write(fd, (line + "\r\n").c_str(), line.size());
	if (flock(fd, 8) != 0) { printf("Giveup to unlock log file %s\n", logFilePath); close(fd); return fd; }
	close(fd);
	return fd;
}
int inLog ( std :: string line , int idx , bool showInConsole ) { int fd = openLogFile ( 3 ) ; if ( fd == -1 ) { printf ( " \n " , logFilePath ) ; return fd ; } if ( flock ( fd , 2 ) != 0 ) { printf ( " \n " , logFilePath ) ; close ( fd ) ; return fd ; } line . insert ( 0 , getHeader ( std :: to_string ( idx ) ) ) ; if ( showInConsole ) { printf ( " \n " , line . c_str ( ) ) ; } write ( fd , ( line + " \r \n " ) . c_str ( ) , line . size ( ) ) ; if ( flock ( fd , 8 ) != 0 ) { printf ( " \n " , logFilePath ) ; close ( fd ) ; return fd ; } close ( fd ) ; return fd ; }
Write to logfile given line, with marked by idx
Write to logfile given line with marked by idx
//trying to open logfile, max 3 times //trying to lock logfile //generating header for log entry and pushing it to buffer //show generated entry in console, if specified //writing data to file //trying to unlock file //close file