Example #1
0
bool AddFile( M_OutputFile& files, const std::string& fileName, Stream stream, ThreadId threadId, bool append )
{
	Helium::MutexScopeLock mutex (g_Mutex);

	M_OutputFile::iterator found = files.find( fileName );
	if ( found != files.end() )
	{
		if ( found->second.m_StreamType != stream )
		{
			HELIUM_BREAK(); // trying to add the same file, but with a different stream type
		}

		if ( found->second.m_ThreadId != threadId )
		{
			HELIUM_BREAK(); // trying to add the same file with a different thread id
		}

		found->second.m_RefCount++; // another reference
	}
	else
	{
		if (fileName != TXT( "" ))
		{
			File* f = new File;
			if ( f->Open( fileName.c_str(), FileModes::Write ) )
			{
				if ( append )
				{
					f->Seek( 0, SeekOrigins::End );
				}

				g_FileManager.Opened( fileName, f );
				OutputFile info;
				info.m_StreamType = stream;
				info.m_RefCount = 1;
				info.m_ThreadId = threadId;
				files[ fileName ] = info;
				return true;
			}
			else
			{
				delete f;
				f = NULL;
			}
		}
	}

	return false;
}
Example #2
0
void RemoveFile( M_OutputFile& files, const tstring& fileName )
{
    Helium::MutexScopeLock mutex (g_Mutex);

    M_OutputFile::iterator found = files.find( fileName );
    if ( found != files.end() )
    {
        found->second.m_RefCount--;

        if ( found->second.m_RefCount == 0 )
        {
            g_FileManager.Close( fileName );
            files.erase( found );
        }
    }
}
Example #3
0
bool AddFile( M_OutputFile& files, const tstring& fileName, Stream stream, uint32_t threadId, bool append )
{
    Helium::MutexScopeLock mutex (g_Mutex);

    M_OutputFile::iterator found = files.find( fileName );
    if ( found != files.end() )
    {
        if ( found->second.m_StreamType != stream )
        {
            HELIUM_BREAK(); // trying to add the same file, but with a different stream type
        }

        if ( found->second.m_ThreadId != threadId )
        {
            HELIUM_BREAK(); // trying to add the same file with a different thread id
        }

        found->second.m_RefCount++; // another reference
    }
    else
    {
        FILE* f = NULL;

        if (fileName != TXT( "" ))
        {
            tchar_t *mode = append ? TXT( "at+" ) : TXT( "wt+" );

            f = _tfopen( fileName.c_str(), mode );
        }

        if ( f )
        {
            g_FileManager.Opened( fileName, f );
            OutputFile info;
            info.m_StreamType = stream;
            info.m_RefCount = 1;
            info.m_ThreadId = threadId;
            files[ fileName ] = info;
            return true;
        }
    }

    return false;
}
Example #4
0
void Log::PrintString(const tchar_t* string, Stream stream, Level level, Color color, int indent, tchar_t* output, uint32_t outputSize)
{
    Helium::MutexScopeLock mutex (g_Mutex);

    // check trace files
    bool trace = false;
    M_OutputFile::iterator itr = g_TraceFiles.begin();
    M_OutputFile::iterator end = g_TraceFiles.end();
    for( ; itr != end; ++itr )
    {
        if ( ( (*itr).second.m_StreamType & stream ) == stream
            && ( (*itr).second.m_ThreadId == -1 || (*itr).second.m_ThreadId == GetCurrentThreadID() ) )
        {
            trace = true;
        }
    }

    // determine if we should be displayed
    bool display = ( g_Streams & stream ) == stream && level <= g_Level;

    // check for nothing to do
    if ( trace || display || output )
    {
        if ( indent < 0 )
        {
            indent = g_Indent;
        }

        // the statement
        Statement statement ( string, stream, level, indent );

        // construct the print statement
        PrintingArgs args ( statement );

        // is this statement to be output via normal channels
        if ( display )
        {
            // raise the printing event
            g_PrintingEvent.Raise( args );
        }

        // only process this string if it was not handled by a handler
        if ( !args.m_Skip )
        {
            // apply indentation
            statement.m_String.clear();
            statement.ApplyIndent( string, statement.m_String );

            // output to screen window
            if ( display )
            {
                // deduce the color if we were told to do so
                if ( color == Colors::Auto )
                {
                    color = GetStreamColor( stream );
                }

                // print the statement to the window
                Helium::PrintString((Helium::ConsoleColor)color, stream == Streams::Error ? stderr : stdout, statement.m_String);

                // raise the printed event
                g_PrintedEvent.Raise( PrintedArgs (statement) );
            }

            // send the text to the debugger, if no debugger nothing happens
            OutputDebugString(statement.m_String.c_str());

            // output to trace file(s)
            static bool stampNewLine = true;

            itr = g_TraceFiles.begin();
            end = g_TraceFiles.end();
            for( ; itr != end; ++itr )
            {
                if ( ( (*itr).second.m_StreamType & stream ) == stream
                    && ( (*itr).second.m_ThreadId == -1 || (*itr).second.m_ThreadId == GetCurrentThreadID() ) )
                {
                    Redirect( (*itr).first, statement.m_String.c_str(), stampNewLine );
                }
            }

            // update stampNewLine
            if ( !statement.m_String.empty() )
            {
                stampNewLine = ( *statement.m_String.rbegin() == '\n' ) ? true : false ;
            }

            // output to buffer
            if (output && outputSize > 0)
            {
                _tcsncpy( output, statement.m_String.c_str(), outputSize - 1 );
            }
        }
    }
}