Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how do i log file in C++

Posted on 2014-12-26
10
300 Views
Last Modified: 2014-12-29
though im unfamilar with C++ & finally i have wrote such code like following with the refeneces of web,but i has failded,the program has been stoke & memory leak,i just cant figure out why

with the web page
https://gist.github.com/To0ki3/3886428
i add this hpp to my program,however it's memory leak,but if i commet the log file code,my program back to normal and works fine,in addition, Is there any problem that i used for a webservice to call this dll?im very concern the memory leak or other exception.im using vs 2013 paid version .

#1 cpgetadd.h
extern "C"
{
      __declspec(dllexport) int  __stdcall callwebaddr(const char * theurl);
}

//#2 cpgetadd.cpp
#include <stdio.h>
#include <curl/curl.h>
#include "cpgetadd.h"
#include <fstream>
#include<string>
#include "FileLogger.hpp"


#pragma comment(lib,"libcurldll.a")

using namespace std;

void write_text_to_log_file(const std::string &text);

int  __stdcall callwebaddr(const char * theurl)
{
      CURL *curl;
      CURLcode res;

      curl = curl_easy_init();
      if (curl)
      {
            curl_easy_setopt(curl, CURLOPT_URL, theurl);
            /* example.com is redirected, so we tell libcurl to follow redirection */
            curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

            /* Perform the request, res will get the return code */
            res = curl_easy_perform(curl);
            /* Check for errors */
            if (res != CURLE_OK)
            {
            //      fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
            //the follwing code will comes memory leak if i uncomment it      
            /*      ige::FileLogger myLog("1.0.4.2", "testfile.txt");

                  // Writing warnings or errors to file is very easy and C++ style
                  size_t slen=strlen(curl_easy_strerror(res))+1;
                  size_t  slenwadd = strlen(theurl) + 1;
                  char * errstr = new char[slen + slenwadd];
                  strcpy_s(errstr, slenwadd, theurl);
                  strcat_s(errstr, slen, curl_easy_strerror(res));
                  myLog << ige::FileLogger::e_logType::LOG_ERROR << errstr;
                  delete[] errstr;*/
                  curl_easy_cleanup(curl);
                  return -1;
            }
            else
            {
                    //the follwing code will comes memory leak if i uncomment it      
      /*            ige::FileLogger myLog("1.0.4.2", "testfile.txt");
                  size_t slenwadd = strlen(theurl) + 1;
            char * errstr = new char[ slenwadd + 8];
                  strcpy_s(errstr, slenwadd, theurl);
                  strcat_s(errstr,7, "success");
                  myLog << errstr;
                  delete[] errstr;
            */

                  curl_easy_cleanup(curl);
                  return 0;
            }

      }
       //the follwing code will comes memory leak if i uncomment it      
      /*ige::FileLogger myLog("1.0.4.2", "testfile.txt");
      size_t slenwadd = strlen(theurl) + 1;
      char * errstring = "initial failed";
      size_t slenstr = strlen(errstring) + 1;
      char * errstr = new char[slenwadd + slenstr];
      strcpy_s(errstr, slenwadd, theurl);
      strcat_s(errstr, slenstr,errstring);
      myLog << ige::FileLogger::e_logType::LOG_ERROR << errstr;
      delete[] errstr;
      delete[] errstring;*/
      return -2;

}

/*this is my another version of log file function,it occured error in std::end when i compile it,i just dont know how to resolve it.*/
/*
void write_text_to_log_file(const std::string &text)
{
      std::ofstream log_file(
            "log_file.txt", std::ios_base::out | std::ios_base::app);
      log_file << text << std::end;
}
*/

many thanks
best regards & happy new year
ken
0
Comment
Question by:ken yup
  • 5
  • 3
  • 2
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40519449
Did you mean std::endl ?
0
 

Author Comment

by:ken yup
ID: 40519548
look at this:
http://www.codeproject.com/Questions/97485/how-to-write-log-file-in-C
i copy the code from here,but it doesnt works
0
 

Author Comment

by:ken yup
ID: 40519550
the function of "void write_text_to_log_file(const std::string &text)" i copy from codeproject
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 84

Assisted Solution

by:ozo
ozo earned 125 total points
ID: 40519688
It looks like they meant to say endl too.
0
 

Author Comment

by:ken yup
ID: 40520152
ok,i'll try to change it,thanks
0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 375 total points
ID: 40521567
however it's memory leak,but if i commet the log file my program back to normal and works fine
the code has much more problems than memory leaks. it is a mix of c and c++ where neither the one nor the other is used with care.

generally, if using std::string and std::ostream, you don't need to create char pointers or use functions like strcpy, strcpy_s, strcat, strlen, ....

if you don't use 'new' operator you won't create heap memory yourself and because of that your code would not have memory leaks.

(1) always initialize variables:

 
CURL *curl = NULL;
 CURLcode res = CURLE_OK;

Open in new window


(2) the logging code should be turned like that:

 ige::FileLogger myLog("1.0.4.2", "testfile.txt");

// Writing warnings or errors to file is very easy and C++ style
myLog << ige::FileLogger::e_logType::LOG_ERROR << "|" << curl_easy_strerror(res) << "|" << theurl << std::endl;

Open in new window


if the code throws an exception, you may check whether curl_easy_strerror(res) returns a non-null char pointer and whether theurl is valid.

generally, for c++ streams you don't need any helper char arrays or string concatenation. simply use the operator<< to add.

Sara
0
 

Author Comment

by:ken yup
ID: 40521679
thank you for your opinions,because i make this dll for powerbuilder legacy version,char * variable is equal string variable in powerbuilder,so i have to use char *,however char * is a C-style character.however i confuse with c & c++,i dont know how to write a dll for powerbuilder correctly.what should be aware.of course my knowledge of c++ is still not enough
0
 
LVL 33

Accepted Solution

by:
sarabande earned 375 total points
ID: 40521775
char * variable is equal string variable in powerbuilde
char * is a pointer to char. if it points to 1st element of a char array which has a zero-element to terminate the string it can be used instead of const std::string& because std::string has a constructor that takes a const char* as argument. additionally std::string has a member function c_str(), which you can use whenever a function needs a const char* type as argument or for the right side of an assignment. if a function needs a (writeable) char * buffer, you may do the following:

// define a string variable 
std::string strvar = "whatever value";
// resize it to maximum size needed.
strvar.resize(512); 
// call function that needs char* argument
function_requiring_writeable_char_pointer(&strvar[0], 512);
strvar.resize(strlen(strvar.c_str()));

Open in new window


Sara
0
 

Author Comment

by:ken yup
ID: 40521788
from your opinion i have another question:
as you metioned, i wrote the API code mixed with c & c++ sytanx,do you mean i should write the API in pure-C sytanx?
0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 375 total points
ID: 40521834
i should write the API in pure-C sytanx?
i would recommend against that beside your requirement is to call the api from pure (ansi) c environments, what means that the projects were built by using c compiler rather than c++ compiler (what rarely is the case for nowadays projects). for all other projects an API using stl types as arguments (instead of pointers to arrays) is the better choice.

Sara
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL400 max size 5 99
PHP question(s) about order of output 9 68
Interview question Javascript, database 12 82
boost::uuid crashes 17 11
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question