Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to create log file and how it use?

How to create log file and how it use?

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Atul23
    wrote on last edited by
    #1

    Hi All I am developing project in VC++ 6.0 and I want to use log file in my application, so this file to be create and how it use in project. Thnaks Atool Atool

    D R D H 4 Replies Last reply
    0
    • A Atul23

      Hi All I am developing project in VC++ 6.0 and I want to use log file in my application, so this file to be create and how it use in project. Thnaks Atool Atool

      D Offline
      D Offline
      dharani
      wrote on last edited by
      #2

      Hi You can use the function below which opens a log.txt in your exe's local folder and writes whatever message you pass thro it . Its also threadsafe . If your application is single threaded then remove CSingleLock stuff ... if flag is 0 then 'msg' is written to log only and not printed in console . If you want to do both - then pass flag =1... int WriteMessage(CString msg,UINT flag){ int ret=0; CSingleLock sLock(&sec);// remove this if your app is single threaded.. sLock.Lock();//remove this if your app is single threaded.. TRY { ret=logfile.Open("log.txt",CStdioFile::modeWrite );// try opening log.txt if(ret==1)// if succeeded { logfile.SeekToEnd();//go to end of log file CTime t = CTime::GetCurrentTime();// time stamp stuff CString tim=t.Format("%m/%d/%Y %H:%M:%S : "); tim=tim+msg+"\n"; logfile.WriteString(tim);//write to log with the message passed thro "msg" logfile.Close();//close the file if(flag) { cout<m_cause << "\n"; #endif } END_CATCH sLock.Unlock();//remove this if your app is single threaded.. return ret; }

      redindian

      A 1 Reply Last reply
      0
      • A Atul23

        Hi All I am developing project in VC++ 6.0 and I want to use log file in my application, so this file to be create and how it use in project. Thnaks Atool Atool

        R Offline
        R Offline
        Rems Kris
        wrote on last edited by
        #3

        You can use std::basic_fstream for creating a log file. You can use open(), write() etc to write to the file. If multiple applications will use the log file, use some synchronization objects, for eg: Mutex.

        1 Reply Last reply
        0
        • D dharani

          Hi You can use the function below which opens a log.txt in your exe's local folder and writes whatever message you pass thro it . Its also threadsafe . If your application is single threaded then remove CSingleLock stuff ... if flag is 0 then 'msg' is written to log only and not printed in console . If you want to do both - then pass flag =1... int WriteMessage(CString msg,UINT flag){ int ret=0; CSingleLock sLock(&sec);// remove this if your app is single threaded.. sLock.Lock();//remove this if your app is single threaded.. TRY { ret=logfile.Open("log.txt",CStdioFile::modeWrite );// try opening log.txt if(ret==1)// if succeeded { logfile.SeekToEnd();//go to end of log file CTime t = CTime::GetCurrentTime();// time stamp stuff CString tim=t.Format("%m/%d/%Y %H:%M:%S : "); tim=tim+msg+"\n"; logfile.WriteString(tim);//write to log with the message passed thro "msg" logfile.Close();//close the file if(flag) { cout<m_cause << "\n"; #endif } END_CATCH sLock.Unlock();//remove this if your app is single threaded.. return ret; }

          redindian

          A Offline
          A Offline
          Atul23
          wrote on last edited by
          #4

          Hi Dharani Thanks for help, but in your code you use logfile object to call WriteString and close(), so how this LogFile object, It belongs to which class? Thanks Atool Atool

          1 Reply Last reply
          0
          • A Atul23

            Hi All I am developing project in VC++ 6.0 and I want to use log file in my application, so this file to be create and how it use in project. Thnaks Atool Atool

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            These should at least get you started: http://www.codeproject.com/file/fixed-length_logging.asp[^] http://www.codeproject.com/debug/logtrace.asp[^] http://www.codeproject.com/ce/GenericLogFunctionality.asp[^]


            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

            "Judge not by the eye but by the heart." - Native American Proverb

            1 Reply Last reply
            0
            • A Atul23

              Hi All I am developing project in VC++ 6.0 and I want to use log file in my application, so this file to be create and how it use in project. Thnaks Atool Atool

              H Offline
              H Offline
              Hamid Taebi
              wrote on last edited by
              #6

              And see http://www.codeproject.com/cpp/logfile.asp[^]


              WhiteSky


              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups