Skip to content

Commit

Permalink
Fixed examples
Browse files Browse the repository at this point in the history
Added clear_traceback method which clears the error stack in a thread safe way.
  • Loading branch information
JosephP91 committed Mar 20, 2017
1 parent 162b98a commit f3c5499
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
6 changes: 0 additions & 6 deletions README.md
Expand Up @@ -70,7 +70,6 @@ int main(int argc, const char **argv) {
curlcpp_traceback errors = error.get_traceback();
// Otherwise we could print the stack like this:
error.print_traceback();
// Note that the printing the stack will erase it
}
return 0;
}
Expand Down Expand Up @@ -108,7 +107,6 @@ int main(int argc, const char **argv) {
curlcpp_traceback errors = error.get_traceback();
// Otherwise we could print the stack like this:
error.print_traceback();
// Note that the printing the stack will erase it
}
// Retrieve information about curl current session.
Expand Down Expand Up @@ -162,7 +160,6 @@ int main(int argc, const char * argv[]) {
curlcpp_traceback errors = error.get_traceback();
// Otherwise we could print the stack like this:
error.print_traceback();
// Note that the printing the stack will erase it
}
return 0;
}
Expand Down Expand Up @@ -210,7 +207,6 @@ int main(int argc, const char * argv[]) {
curlcpp_traceback errors = error.get_traceback();
// Otherwise we could print the stack like this:
error.print_traceback();
// Note that the printing the stack will erase it
}
myfile.close();
return 0;
Expand Down Expand Up @@ -256,7 +252,6 @@ int main() {
curlcpp_traceback errors = error.get_traceback();
// Otherwise we could print the stack like this:
error.print_traceback();
// Note that the printing the stack will erase it
}
// Let's print the stream content.
cout<<str.str()<<endl;
Expand Down Expand Up @@ -305,7 +300,6 @@ int main(int argc, const char * argv[]) {
curlcpp_traceback errors = error.get_traceback();
// Otherwise we could print the stack like this:
error.print_traceback();
// Note that the printing the stack will erase it
}

// Creation of a sender. You should wait here using select to check if socket is ready to send.
Expand Down
11 changes: 11 additions & 0 deletions include/curl_exception.h
Expand Up @@ -69,6 +69,10 @@ namespace curl {
* Simple method which prints the entire error stack.
*/
void print_traceback() const;
/**
* Simple method which clears the entire error stack.
*/
void clear_traceback() const;
private:
/**
* The error container must be static or will be cleared
Expand All @@ -88,6 +92,13 @@ namespace curl {
std::cout<<"ERROR: "<<value.first<<" ::::: FUNCTION: "<<value.second<<std::endl;
});
}

// Implementation of clear method.
inline void curl_exception::clear_traceback() const {
curl_exception::tracebackLocker.lock();
curl_exception::traceback.clear();
curl_exception::tracebackLocker.unlock();
}

// Implementation of get_traceback.
inline curlcpp_traceback curl_exception::get_traceback() const {
Expand Down
6 changes: 2 additions & 4 deletions src/curl_exception.cpp
Expand Up @@ -8,17 +8,15 @@
using curl::curl_exception;
using curl::curlcpp_traceback;

// Need to define the traceback here to separate declaration from definition, or we'll get a linker error.
// Need to define the traceback here to separate declaration from definition, or we'll get a linker error...
curlcpp_traceback curl::curl_exception::traceback;

// .. same for the traceback mutexe.
std::mutex curl::curl_exception::tracebackLocker;

// Constructor implementation. Every call will push into the calls stack the function name and the error occurred.
curl_exception::curl_exception(const std::string &error, const std::string &fun_name) {
curl_exception::tracebackLocker.lock();

curl_exception::traceback.insert(curl_exception::traceback.begin(),curlcpp_traceback_object(error,fun_name));

curl_exception::tracebackLocker.unlock();
}

Expand Down
4 changes: 1 addition & 3 deletions test/easy.cpp
Expand Up @@ -4,8 +4,7 @@ using curl::curl_easy;
using curl::curl_easy_exception;

/*
* This example shows how to make a simple request with
* curl.
* This example shows how to make a simple request with curl.
*/

int main() {
Expand All @@ -22,7 +21,6 @@ int main() {
auto errors = error.what();
// Otherwise we could print the stack like this:
error.print_traceback();
// Note that the printing the stack will erase it
}
return 0;
}
1 change: 0 additions & 1 deletion test/easy_info.cpp
Expand Up @@ -27,7 +27,6 @@ int main(int argc, const char **argv) {
curlcpp_traceback errors = error.get_traceback();
// Otherwise we could print the stack like this:
error.print_traceback();
// Note that the printing the stack will erase it
}
// Retrieve information about curl current session.
auto x = easy.get_info<char>(CURLINFO_CONTENT_TYPE);
Expand Down

1 comment on commit f3c5499

@JosephP91
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solves #99

Please sign in to comment.