ken yup
asked on
how do i log file in C++
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(con st 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_stre rror(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_ERRO R << 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_ERRO R << 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(con st 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
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(con
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_stre
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
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
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(con
{
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
Did you mean std::endl ?
ASKER
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
http://www.codeproject.com/Questions/97485/how-to-write-log-file-in-C
i copy the code from here,but it doesnt works
ASKER
the function of "void write_text_to_log_file(con st std::string &text)" i copy from codeproject
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok,i'll try to change it,thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.