bool Log::init ( char *filename ) { // set the main process id s_pid = getpidtid(); // init these m_numErrors = 0; m_bufPtr = 0; m_fd = -1; m_disabled = false; #ifdef DEBUG g_dbufSize = 4096; g_dbuf = (char*)mmalloc(g_dbufSize,"Log: DebugBuffer"); if (!g_dbuf) fprintf(stderr, "Unable to init debug buffer"); #endif // m_hostname = g_conf.m_hostname; // m_port = port; // is there a filename to log our errors to? m_filename = filename; if ( ! m_filename ) return true; // skip this for now return true; // open it for appending. // create with -rw-rw-r-- permissions if it's not there. m_fd = open ( m_filename , O_APPEND | O_CREAT | O_RDWR , S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH ); if ( m_fd >= 0 ) return true; // bitch to stderr and return false on error fprintf(stderr,"could not open log file %s for appending\n",m_filename); return false; }
void Log::setPid ( ) { s_pid = getpidtid(); }
bool Log::logR ( long long now , long type , char *msg , bool asterisk , bool forced ) { // filter if we should //if ( forced ) goto skipfilter; // return true if we should not log this if ( ! forced && ! shouldLog ( type , msg ) ) return true; // skipfilter: // can we log if we're a sig handler? don't take changes if ( g_inSigHandler ) return logLater ( now , type , msg , NULL ); //if ( g_inSigHandler ) return false; // get "msg"'s length long msgLen = gbstrlen ( msg ); #ifdef PTHREADS // lock for threads pthread_mutex_lock ( &s_lock ); #endif // do a timestamp, too. use the time synced with host #0 because // it is easier to debug because all log timestamps are in sync. if ( now == 0 ) now = gettimeofdayInMillisecondsGlobalNoCore(); // . skip all logging if power out, we do not want to screw things up // . allow logging for 10 seconds after power out though if ( ! g_process.m_powerIsOn && now - g_process.m_powerOffTime >10000){ #ifdef PTHREADS pthread_mutex_unlock ( &s_lock ); #endif return false; } //if ( now == 0 ) now = g_nowApprox; // chop off any spaces at the end of the msg. while ( is_wspace_a ( msg [ msgLen - 1 ] ) && msgLen > 0 ) msgLen--; // get this pid pid_t pid = getpidtid(); // a tmp buffer char tt [ MAX_LINE_LEN ]; char *p = tt; char *pend = tt + MAX_LINE_LEN; /* // print timestamp, hostid, type if ( g_hostdb.m_numHosts <= 999 ) sprintf ( p , "%llu %03li %s ", now , g_hostdb.m_hostId , getTypeString(type) ); else if ( g_hostdb.m_numHosts <= 9999 ) sprintf ( p , "%llu %04li %s ", now , g_hostdb.m_hostId , getTypeString(type) ); else if ( g_hostdb.m_numHosts <= 99999 ) sprintf ( p , "%llu %05li %s ", now , g_hostdb.m_hostId , getTypeString(type) ); */ // print timestamp, hostid, type if ( m_logTimestamps ) { if ( g_hostdb.m_numHosts <= 999 ) sprintf ( p , "%llu %03li ", now , g_hostdb.m_hostId ); else if ( g_hostdb.m_numHosts <= 9999 ) sprintf ( p , "%llu %04li ", now , g_hostdb.m_hostId ); else if ( g_hostdb.m_numHosts <= 99999 ) sprintf ( p , "%llu %05li ", now , g_hostdb.m_hostId ); p += gbstrlen ( p ); } // msg resource char *x = msg; long cc = 7; // the first 7 bytes or up to the : must be ascii //while ( p < pend && *x && is_alnum_a(*x) ) { *p++ = *x++; cc--; } // space pad //while ( cc-- > 0 ) *p++ = ' '; // ignore the label for now... while ( p < pend && *x && is_alnum_a(*x) ) { x++; cc--; } // thread id if in "thread" if ( pid != s_pid && s_pid != -1 ) { //sprintf ( p , "[%li] " , (long)getpid() ); sprintf ( p , "[%lu] " , (unsigned long)pid ); p += gbstrlen ( p ); } // then message itself long avail = (MAX_LINE_LEN) - (p - tt) - 1; if ( msgLen > avail ) msgLen = avail; if ( *x == ':' ) x++; if ( *x == ' ' ) x++; strncpy ( p , x , avail ); // capitalize for consistency. no, makes grepping log msgs harder. //if ( is_alpha_a(*p) ) *p = to_upper_a(*p); p += gbstrlen(p); // back up over spaces while ( p[-1] == ' ' ) p--; // end in period or ? or ! //if ( p[-1] != '?' && p[-1] != '.' && p[-1] != '!' ) // *p++ = '.'; *p ='\0'; // the total length, not including the \0 long tlen = p - tt; // call sprintf, but first make sure we have room in m_buf and in // the arrays. who know how much room the sprintf is going to need??? // NOTE: TODO: this is shaky -- fix it! if ( m_bufPtr + tlen >= 1024 * 32 || m_numErrors >= MAX_LOG_MSGS){ // this sets m_bufPtr to 0 if ( ! dumpLog ( ) ) { fprintf(stderr,"Log::log: could not dump to file!\n"); #ifdef PTHREADS pthread_mutex_unlock ( &s_lock ); #endif return false; } } // . filter out nasty chars from the message // . replace with ~'s char cs; char *ttp = tt; char *ttpend = tt + tlen; for ( ; ttp < ttpend ; ttp += cs ) { cs = getUtf8CharSize ( ttp ); if ( is_binary_utf8 ( ttp ) ) { for ( long k = 0 ; k < cs ; k++ ) *ttp++ = '.'; // careful not to skip the already skipped bytes cs = 0; continue; } // convert \n's and \r's to spaces if ( *ttp == '\n' ) *ttp = ' '; if ( *ttp == '\r' ) *ttp = ' '; if ( *ttp == '\t' ) *ttp = ' '; } if ( m_fd >= 0 ) { write ( m_fd , tt , tlen ); write ( m_fd , "\n", 1 ); } else { // print it out for now fprintf ( stderr, "%s\n", tt ); } // set the stuff in the array m_errorMsg [m_numErrors] = msg; m_errorMsgLen [m_numErrors] = msgLen; m_errorTime [m_numErrors] = now; m_errorType [m_numErrors] = type; // increase the # of errors m_numErrors++; #ifdef PTHREADS // unlock for threads pthread_mutex_unlock ( &s_lock ); #endif return false; }
void Images::thumbStart_r ( bool amThread ) { long long start = gettimeofdayInMilliseconds(); //static char scmd[200] = "%stopnm %s | " // "pnmscale -xysize 100 100 - | " // "ppmtojpeg - > %s"; log( LOG_DEBUG, "image: thumbStart_r entered." ); //DIR *d; //char cmd[2500]; //sprintf( cmd, "%strash", g_hostdb.m_dir ); makeTrashDir(); // get thread id long id = getpidtid(); // pass the input to the program through this file // rather than a pipe, since popen() seems broken char in[364]; snprintf ( in , 363,"%strash/in.%li", g_hostdb.m_dir, id ); unlink ( in ); log( LOG_DEBUG, "image: thumbStart_r create in file." ); // collect the output from the filter from this file char out[364]; snprintf ( out , 363,"%strash/out.%li", g_hostdb.m_dir, id ); unlink ( out ); log( LOG_DEBUG, "image: thumbStart_r create out file." ); // ignore errno from those unlinks errno = 0; // Open/Create temporary file to store image to int fhndl; if( (fhndl = open( in, O_RDWR+O_CREAT, S_IWUSR+S_IRUSR )) < 0 ) { log( "image: Could not open file, %s, for writing: %s - %d.", in, mstrerror( m_errno ), fhndl ); m_imgDataSize = 0; return; } // Write image data into temporary file if( write( fhndl, m_imgData, m_imgDataSize ) < 0 ) { log( "image: Could not write to file, %s: %s.", in, mstrerror( m_errno ) ); close( fhndl ); unlink( in ); m_imgDataSize = 0; return; } // Close temporary image file now that we have finished writing if( close( fhndl ) < 0 ) { log( "image: Could not close file, %s, for writing: %s.", in, mstrerror( m_errno ) ); unlink( in ); m_imgDataSize = 0; return; } fhndl = 0; // Grab content type from mime //long imgType = mime.getContentType(); char ext[5]; switch( m_imgType ) { case CT_GIF: strcpy( ext, "gif" ); break; case CT_JPG: strcpy( ext, "jpeg" ); break; case CT_PNG: strcpy( ext, "png" ); break; case CT_TIFF: strcpy( ext, "tiff" ); break; case CT_BMP: strcpy( ext, "bmp" ); break; } long xysize = 250;//100; // make thumbnail a little bigger for diffbot for widget if ( m_xd->m_isDiffbotJSONObject ) xysize = 250; // i hope 2500 is big enough! char cmd[2501]; //sprintf( cmd, scmd, ext, in, out); char *wdir = g_hostdb.m_dir; snprintf( cmd, 2500 , "LD_LIBRARY_PATH=%s %s/%stopnm %s | " "LD_LIBRARY_PATH=%s %s/pnmscale -xysize %li %li - | " "LD_LIBRARY_PATH=%s %s/ppmtojpeg - > %s" , wdir , wdir , ext , in , wdir , wdir , xysize , xysize , wdir , wdir , out ); // Call clone function for the shell to execute command // This call WILL BLOCK . timeout is 30 seconds. //int err = my_system_r( cmd, 30 ); // m_thmbconvTimeout ); int err = system( cmd ); // m_thmbconvTimeout ); //if( (m_dx != 0) && (m_dy != 0) ) // unlink( in ); unlink ( in ); if ( err == 127 ) { m_errno = EBADENGINEER; log("image: /bin/sh does not exist."); unlink ( out ); m_stopDownloading = true; return; } // this will happen if you don't upgrade glibc to 2.2.4-32 or above if ( err != 0 ) { m_errno = EBADENGINEER; log("image: Call to system(\"%s\") had error.",cmd); unlink ( out ); m_stopDownloading = true; return; } // Open new file with thumbnail image if( (fhndl = open( out, O_RDONLY )) < 0 ) { log( "image: Could not open file, %s, for reading: %s.", out, mstrerror( m_errno ) ); m_stopDownloading = true; return; } if( (m_thumbnailSize = lseek( fhndl, 0, SEEK_END )) < 0 ) { log( "image: Seek of file, %s, returned invalid size: %ld", out, m_thumbnailSize ); m_stopDownloading = true; close(fhndl); return; } if( m_thumbnailSize > m_imgReplyMaxLen ) { log(LOG_DEBUG,"image: Image thumbnail larger than buffer!" ); log(LOG_DEBUG,"image: File Read Bytes: %ld", m_thumbnailSize); log(LOG_DEBUG,"image: Buf Max Bytes : %ld",m_imgReplyMaxLen ); log(LOG_DEBUG,"image: -----------------------" ); log(LOG_DEBUG,"image: Diff : %ld", m_imgReplyMaxLen-m_thumbnailSize ); close(fhndl); return; } if( lseek( fhndl, 0, SEEK_SET ) < 0 ) { log( "image: Seek couldn't rewind file, %s.", out ); m_stopDownloading = true; close(fhndl); return; } // . Read contents back into image ptr // . this is somewhat of a hack since it overwrites the original img if( (m_thumbnailSize = read( fhndl, m_imgData, m_imgDataSize )) < 0 ) { log( "image: Could not read from file, %s: %s.", out, mstrerror( m_errno ) ); close( fhndl ); m_stopDownloading = true; unlink( out ); return; } if( close( fhndl ) < 0 ) { log( "image: Could not close file, %s, for reading: %s.", out, mstrerror( m_errno ) ); unlink( out ); m_stopDownloading = true; return; } fhndl = 0; unlink( out ); long long stop = gettimeofdayInMilliseconds(); // tell the loop above not to download anymore, we got one m_thumbnailValid = true; // MDW: this was m_imgReply getImageInfo ( m_imgData , m_thumbnailSize , &m_tdx , &m_tdy , NULL ); // now make the meta data struct // <imageUrl>\0<width><height><thumbnailData> log( LOG_DEBUG, "image: Thumbnail size: %li bytes.", m_imgDataSize ); log( LOG_DEBUG, "image: Thumbnail dx=%li dy=%li.", m_tdx,m_tdy ); log( LOG_DEBUG, "image: Thumbnail generated in %lldms.", stop-start ); }