int DebugThis(int level) { if (!(level & GetDebugLevel())) return 0; return 1; }
void CId2Reader::x_ConnectAtSlot(TConn conn) { if ( GetDebugLevel() >= eTraceOpen ) { CDebugPrinter s(conn, "CId2Reader"); s << "New connection to " << m_Connector.GetServiceName() << "..."; } CReaderServiceConnector::SConnInfo conn_info = m_Connector.Connect(); CConn_IOStream& stream = *conn_info.m_Stream; SetRandomFail(stream, conn); if ( stream.bad() ) { NCBI_THROW(CLoaderException, eConnectionFailed, "cannot open connection: "+x_ConnDescription(stream)); } if ( GetDebugLevel() >= eTraceOpen ) { CDebugPrinter s(conn, "CId2Reader"); s << "New connection: " << x_ConnDescription(stream); } try { x_InitConnection(stream, conn); } catch ( CException& exc ) { m_Connector.RememberIfBad(conn_info); NCBI_RETHROW(exc, CLoaderException, eConnectionFailed, "connection initialization failed: "+ x_ConnDescription(stream)); } SetRandomFail(stream, conn); if ( stream.bad() ) { NCBI_THROW(CLoaderException, eConnectionFailed, "connection initialization failed: "+ x_ConnDescription(stream)); } // successfully received reply, server is good, forget it conn_info.MarkAsGood(); STimeout tmout; m_Connector.SetTimeoutTo(&tmout); CONN_SetTimeout(stream.GetCONN(), eIO_ReadWrite, &tmout); tmout.sec = 0; tmout.usec = 1; // no wait on close CONN_SetTimeout(stream.GetCONN(), eIO_Close, &tmout); m_Connections[conn] = conn_info; }
void CId2Reader::x_DisconnectAtSlot(TConn conn, bool failed) { _ASSERT(m_Connections.count(conn)); CReaderServiceConnector::SConnInfo& conn_info = m_Connections[conn]; m_Connector.RememberIfBad(conn_info); if ( conn_info.m_Stream ) { LOG_POST_X(1, Warning << "CId2Reader("<<conn<<"): ID2" " GenBank connection "<<(failed? "failed": "too old")<< ": reconnecting..."); if ( GetDebugLevel() >= eTraceOpen ) { CDebugPrinter s(conn, "CId2Reader"); s << "Closing ID2 connection"; } conn_info.m_Stream.reset(); if ( GetDebugLevel() >= eTraceOpen ) { CDebugPrinter s(conn, "CId2Reader"); s << "Closed ID2 connection"; } } }
CDbgView::CDbgView(_In_ CParentWnd* pParentWnd): CEditor(pParentWnd, IDS_DBGVIEW, NULL, 0, CEDITOR_BUTTON_ACTION1 | CEDITOR_BUTTON_ACTION2, IDS_CLEAR, IDS_CLOSE, NULL) { TRACE_CONSTRUCTOR(CLASS); CreateControls(DBGVIEW_NUMFIELDS); InitPane(DBGVIEW_TAGS, CreateSingleLinePane(IDS_REGKEY_DEBUG_TAG, NULL, false)); SetHex(DBGVIEW_TAGS,GetDebugLevel()); InitPane(DBGVIEW_PAUSE, CreateCheckPane(IDS_PAUSE, false, false)); InitPane(DBGVIEW_VIEW, CreateMultiLinePane(NULL, NULL, true)); m_bPaused = false; DisplayParentedDialog(pParentWnd,800); } // CDbgView::CDbgView
void CCacheWriter::x_WriteId(const string& key, const string& subkey, const char* data, size_t size) { try { if ( GetDebugLevel() > 0 ) { CReader::CDebugPrinter s("CCacheWriter"); s<<key<<","<<subkey<<" size="<<size; } m_IdCache->Store(key, 0, subkey, data, size); } catch ( exception& ) { // ignored } }
static int file_process( void* p, uint8_t* file_data, int data_size, FilePosition position, bool upload) { FileContext* context; Packet *pkt = (Packet *)p; void *ssnptr = pkt->ssnptr; /* if both disabled, return immediately*/ if ((!file_type_id_enabled) && (!file_signature_enabled)) return 0; #if defined(DEBUG_MSGS) && !defined (REG_TEST) if (DEBUG_FILE & GetDebugLevel()) #endif #if defined(DEBUG_MSGS) || defined (REG_TEST) DumpHex(stdout, file_data, data_size); DEBUG_WRAP(DebugMessage(DEBUG_FILE, "stream pointer %p\n", ssnptr ););
void CCacheWriter::WriteSeq_ids(const string& key, const CLoadLockSeqIds& lock) { if( !m_IdCache) { return; } if ( !lock.IsLoaded() || lock.GetExpType() != GBL::eExpire_normal ) { return; } try { if ( GetDebugLevel() > 0 ) { CReader::CDebugPrinter s("CCacheWriter"); s<<key<<","<<GetSeq_idsSubkey(); } _ASSERT(!lock.GetSeq_ids().empty()); auto_ptr<IWriter> writer (m_IdCache->GetWriteStream(key, 0, GetSeq_idsSubkey())); if ( !writer.get() ) { return; } CWStream w_stream(writer.release(), 0, 0, CRWStreambuf::fOwnAll); CObjectOStreamAsnBinary obj_stream(w_stream); CFixedSeq_ids seq_ids = lock.GetSeq_ids(); static_cast<CObjectOStream&>(obj_stream). // cast because of protected WriteUint4(CStoreBuffer::ToUint4(seq_ids.size())); ITERATE ( CFixedSeq_ids, it, seq_ids ) { obj_stream << *it->GetSeqId(); } } catch ( exception& ) { // In case of an error we need to remove incomplete data // from the cache. try { m_IdCache->Remove(key, 0, GetSeq_idsSubkey()); } catch ( exception& ) { // ignored } // ignore cache write error - it doesn't affect application } }
void DebugMessageFunc(int level, char *fmt, ...) { va_list ap; if (!(level & GetDebugLevel())) return; va_start(ap, fmt); if ((barnyard2_conf != NULL) && BcDaemonMode()) { char buf[STD_BUF]; int buf_len = sizeof(buf); char *buf_ptr = buf; buf[buf_len - 1] = '\0'; /* filename and line number information */ if (DebugMessageFile != NULL) { snprintf(buf, buf_len - 1, "%s:%d: ", DebugMessageFile, DebugMessageLine); buf_ptr += strlen(buf); buf_len -= strlen(buf); } vsnprintf(buf_ptr, buf_len - 1, fmt, ap); syslog(LOG_DAEMON | LOG_DEBUG, "%s", buf); } else { if (DebugMessageFile != NULL) printf("%s:%d: ", DebugMessageFile, DebugMessageLine); vprintf(fmt, ap); } va_end(ap); }
void DebugWideMessageFunc(int level, wchar_t *fmt, ...) { va_list ap; wchar_t buf[STD_BUF+1]; if (!(level & GetDebugLevel())) { return; } buf[STD_BUF]= (wchar_t)0; /* filename and line number information */ if (DebugMessageFile != NULL) printf("%s:%d: ", DebugMessageFile, DebugMessageLine); va_start(ap, fmt); if(BcDaemonMode()) { #ifdef WIN32 _vsnwprintf(buf, STD_BUF, fmt, ap); #else #ifdef HAVE_VSWPRINTF vswprintf(buf, STD_BUF, fmt, ap); #endif #endif //syslog(LOG_DAEMON | LOG_DEBUG, "%s", buf); } else { #ifdef HAVE_WPRINTF vwprintf(fmt, ap); #endif } va_end(ap); }
void CId2Reader::x_InitConnection(CConn_IOStream& stream, TConn conn) { // prepare init request CID2_Request req; req.SetRequest().SetInit(); x_SetContextData(req); CID2_Request_Packet packet; packet.Set().push_back(Ref(&req)); // that's it for now // TODO: add params // send init request {{ if ( GetDebugLevel() >= eTraceConn ) { CDebugPrinter s(conn, "CId2Reader"); s << "Sending"; if ( GetDebugLevel() >= eTraceASN ) { s << ": " << MSerial_AsnText << packet; } else { s << " ID2-Request-Packet"; } s << "..."; } try { stream << MConnFormat << packet << flush; } catch ( CException& exc ) { NCBI_RETHROW(exc, CLoaderException, eConnectionFailed, "failed to send init request: "+ x_ConnDescription(stream)); } if ( GetDebugLevel() >= eTraceConn ) { CDebugPrinter s(conn, "CId2Reader"); s << "Sent ID2-Request-Packet."; } if ( !stream ) { NCBI_THROW(CLoaderException, eConnectionFailed, "failed to send init request: "+ x_ConnDescription(stream)); } }} // receive init reply CID2_Reply reply; {{ if ( GetDebugLevel() >= eTraceConn ) { CDebugPrinter s(conn, "CId2Reader"); s << "Receiving ID2-Reply..."; } stream >> MConnFormat >> reply; if ( GetDebugLevel() >= eTraceConn ) { CDebugPrinter s(conn, "CId2Reader"); s << "Received"; if ( GetDebugLevel() >= eTraceASN ) { s << ": " << MSerial_AsnText << reply; } else { s << " ID2-Reply."; } } if ( !stream ) { NCBI_THROW(CLoaderException, eLoaderFailed, "failed to receive init reply: "+ x_ConnDescription(stream)); } }} // check init reply if ( reply.IsSetDiscard() ) { NCBI_THROW(CLoaderException, eLoaderFailed, "bad init reply: 'discard' is set: "+ x_ConnDescription(stream)); } if ( reply.IsSetError() ) { NCBI_THROW(CLoaderException, eLoaderFailed, "bad init reply: 'error' is set: "+ x_ConnDescription(stream)); } if ( !reply.IsSetEnd_of_reply() ) { NCBI_THROW(CLoaderException, eLoaderFailed, "bad init reply: 'end-of-reply' is not set: "+ x_ConnDescription(stream)); } if ( reply.GetReply().Which() != CID2_Reply::TReply::e_Init ) { NCBI_THROW(CLoaderException, eLoaderFailed, "bad init reply: 'reply' is not 'init': "+ x_ConnDescription(stream)); } // that's it for now // TODO: process params }
bool Win32Vif::Write(const char* buffer, unsigned int numBytes) { // ljt is this quite right?? DWORD bytesWritten = 0; OVERLAPPED overlappedPtr; // set up overlapped structure fields overlappedPtr.Offset = 0; overlappedPtr.OffsetHigh = 0; overlappedPtr.hEvent = NULL; const char* bufPtr = buffer; if (NULL != GetNotifier()) { if (!output_ready) { if (FALSE != GetOverlappedResult(tap_handle,&write_overlapped,&bytesWritten,FALSE)) { output_ready = true; overlappedPtr = write_overlapped; bufPtr = write_buffer; if (numBytes > BUFFER_MAX) numBytes = BUFFER_MAX; memcpy(write_buffer,buffer,numBytes); } else { if (GetDebugLevel() >= 2) PLOG(PL_ERROR,"Win32Vif::Write() GetOverlappedResult() error: %s\n",::GetErrorString()); return false; } } } if (0 != WriteFile(tap_handle,bufPtr,numBytes,&bytesWritten,(LPOVERLAPPED)&overlappedPtr)) { numBytes = bytesWritten; if (0 == numBytes) { output_ready = false; } return true; } else { numBytes = 0; switch (GetLastError()) { case ERROR_IO_PENDING: return true; case ERROR_BROKEN_PIPE: OnNotify(NOTIFY_NONE); break; default: PLOG(PL_ERROR,"Win32Vif::Write() WriteFile() error(%d): %s\n",GetLastError(),::GetErrorString()); break; } return false; } return true; } // end Win32Vif::Write()
int main ( int argc, char *argv[], char *envp[]) { int ret; /**********************************/ /* Read in command-line arguments */ /**********************************/ /* FIXME: Argument for daemonizing or not */ /* FIXME: Argument for debug level */ /* FIXME: Arguments affecting the log files, whether to use syslog, etc. (Read conf file?) */ /* Report our debug level */ if ( GetDebugLevel() > DEBUG_NONE) { DbgLog(GetDebugLevel(), "Starting with debugging messages logged at level %d (%d = No messages; %d = few; %d = more, etc.)", GetDebugLevel(), DEBUG_NONE, DEBUG_LEVEL0, DEBUG_LEVEL1); } /* Save our startup directory */ SaveStartupDirectory( argv[0] ); ret = load_and_parse(OCK_CONFIG); if (ret != 0) { ErrLog("Failed to read config file.\n"); return 1; } else DbgLog (DL0, "Parse config file succeeded.\n"); /* Allocate and Attach the shared memory region */ if ( ! CreateSharedMemory() ) { /* CreateSharedMemory() does it's own error logging */ return 1; } DbgLog(DL0,"SHMID %d token %#X \n", shmid, tok); /* Now that we've created the shared memory segment, we attach to it */ if ( ! AttachToSharedMemory() ) { /* AttachToSharedMemory() does it's own error logging */ DestroySharedMemory(); return 2; } /* Initialize the global shared memory mutex (and the attribute used to create the per-process mutexes */ if ( ! InitializeMutexes() ) { DetachFromSharedMemory(); DestroySharedMemory(); return 3; } /* Get the global shared memory mutex */ XProcLock(); /* Populate the Shared Memory Region */ if ( ! InitSharedMemory(shmp) ) { XProcUnLock(); DetachFromSharedMemory(); DestroySharedMemory(); return 4; } /* Release the global shared memory mutex */ XProcUnLock(); if ((socketfd = CreateListenerSocket()) < 0) { DestroyMutexes(); DetachFromSharedMemory(); DestroySharedMemory(); return 5; } if (!InitSocketData(&socketData)) { DetachSocketListener(socketfd); DestroyMutexes(); DetachFromSharedMemory(); DestroySharedMemory(); return 6; } /* * Become a Daemon, if called for */ if ( Daemon ) { pid_t pid; if ( (pid = fork()) < 0 ){ DetachSocketListener(socketfd); DestroyMutexes(); DetachFromSharedMemory(); DestroySharedMemory(); return 7; } else { if ( pid != 0) { exit(0); // Terminate the parent } else { setsid(); // Session leader #ifndef DEV fclose(stderr); fclose(stdout); fclose(stdin); #endif } } } else { #ifdef DEV // Log only on development builds LogLog("Not becoming a daemon...\n"); #endif } /***************************************** * * Register Signal Handlers * Daemon probably should ignore ALL signals possible, since termination * while active is a bad thing... however one could check for * any processes active in the shared memory, and destroy the shm if * the process wishes to terminate. * *****************************************/ /* * We have to set up the signal handlers after we daemonize because * the daemonization process redefines our handler for (at least) SIGTERM */ if ( ! SetupSignalHandlers() ) { DetachSocketListener(socketfd); DestroyMutexes(); DetachFromSharedMemory(); DestroySharedMemory(); return 8; } /* ultimatly we will create a couple of threads which monitor the slot db and handle the insertion and removal of tokens from the slot. */ /* For Testing the Garbage collection routines */ /* shmp->proc_table[3].inuse = TRUE; shmp->proc_table[3].proc_id = 24328; */ #if !defined(NOGARBAGE) printf("Start garbage \n"); /* start garbage collection thread */ if ( ! StartGCThread(shmp) ) { DetachSocketListener(socketfd); DestroyMutexes(); DetachFromSharedMemory(); DestroySharedMemory(); return 9; } #endif // We've fully become a daemon. Now create the PID file { FILE *pidfile; pidfile = fopen(PID_FILE_PATH,"w"); if (pidfile) { fprintf(pidfile,"%d",getpid()); fclose(pidfile); } } while (1) { #if !(THREADED) && !(NOGARBAGE) CheckForGarbage(shmp); #endif SocketConnectionHandler(socketfd, 10); } /************************************************************* * * Here we need to actualy go through the processes and verify that thye * still exist. If not, then they terminated with out properly calling * C_Finalize and therefore need to be removed from the system. * Look for a system routine to determine if the shared memory is held by * the process to further verify that the proper processes are in the * table. * *************************************************************/ } /* end main */