Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » MSIL » General » Downloads
 
Add your own
alternative version

ILRewriting for beginners

, 27 Sep 2012 Ms-PL
Runtime IL-Rewriting can be used to add behavior such as logging to applications, or redirect calls from one API to another. This article and accompanying source code explains how to substitute a method call at runtime.
ILRewriting_demo-noexe.zip
ILRewriting_demo
ILRewriting_demo.zip
ILRewriteProfiler.dll
InterceptApp.exe
InterceptLib.dll
SampleApp1.exe
ILRewriting_src.zip
ILRewriting_src
ILEmitTest
Properties
ILRewriteProfiler
exports.def
ILRewriteProfiler.vcxproj.filters
ILRewriteProfiler.vcxproj.user
InterceptApp
app.aps
app.ico
InterceptApp.vcxproj.filters
InterceptApp.vcxproj.user
InterceptLib
InterceptLib.snk
Properties
SampleApp1
Properties
SampleApp1.snk
// ----------------------------------------------------------------------------------------------
// Copyright (c) Mattias H�gstr�m.
// ----------------------------------------------------------------------------------------------
// This source code is subject to terms and conditions of the Microsoft Public License. A 
// copy of the license can be found in the License.html file at the root of this distribution. 
// If you cannot locate the Microsoft Public License, please send an email to 
// dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
// by the terms of the Microsoft Public License.
// ----------------------------------------------------------------------------------------------
// You must not remove this notice, or any other, from this software.
// ----------------------------------------------------------------------------------------------

#pragma once

#include <Windows.h>
#include <ostream>
#include <sstream>
#include <fstream>
#include <iomanip>

class FileLogger : public std::ostream
{
private:
    class StreamBuffer: public std::stringbuf
    {
    public:
        StreamBuffer(std::ostream& str) : m_out(str), m_useOutputDebug(false) {};   
        StreamBuffer(std::ostream& str, std::string prefix) : m_out(str), m_prefix(prefix), m_useOutputDebug(false) {};
        ~StreamBuffer() {}
        void SetPrefix(const char* linePrefix)
        {
            m_prefix = std::string(linePrefix);
        }
        void Echo2OutputDebug(bool activate) { m_useOutputDebug = activate; }
        virtual int sync()
        {
            if (m_useOutputDebug)
            {       
                std::string tempStr = m_prefix + str();
                OutputDebugStringA(tempStr.c_str());
            }
            if (m_out.good())
            {
                m_out << str();
            }            
            str("");
            return 0;
        }
    private:
        bool m_useOutputDebug;
        std::ostream &m_out;
        std::string m_prefix;
    };

public:
    FileLogger(std::ostream& _ostream) : std::ostream(&m_streamBuffer), m_streamBuffer(_ostream) {}
    FileLogger(std::ostream& _ostream, std::string prefix) : std::ostream(&m_streamBuffer), m_streamBuffer(_ostream, prefix) {}
    FileLogger() : std::ostream(&m_streamBuffer), m_streamBuffer(m_file) {}
    ~FileLogger() { this->Close(); }
    bool IsActive() { return m_file.is_open() || m_useOutputDebug; }
    bool IsOpen() { return m_file.is_open(); }
    void Close();
    void Open(const char* fileName);        
    void WriteLine(const char* message);
    FileLogger& WriteHex64(__int64 val)
    {
        *this << hex << std::setw(8) << std::setfill('0') << val;
        return *this;
    }
    void Echo2OutputDebug(bool activate) { m_useOutputDebug = activate; m_streamBuffer.Echo2OutputDebug(activate); }
    void SetPrefix(const char* linePrefix);
private:
    bool m_useOutputDebug;
    std::ofstream m_file;
    std::string m_fileName;
    std::string m_prefix;
    StreamBuffer m_streamBuffer;
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Mattias Högström
Architect Visma Software AB
Sweden Sweden
Mattias works at Visma, a leading Nordic ERP solution provider. He has good knowledge in C++/.Net development, test tool development, and debugging. His great passion is memory dump analysis. He likes giving talks and courses.
Follow on   Twitter

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 27 Sep 2012
Article Copyright 2012 by Mattias Högström
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid