Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
A donation makes a contribution towards the costs, the time and effort that's going in this site and building.
Thank You! Steffen
Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
| |
|
Topic: How can I write to a file in an c++ Apache module? |
|
Author |
|
joaolourenco82
Joined: 13 Jan 2018 Posts: 1 Location: Portugal, Lisbon
|
Posted: Sun 14 Jan '18 0:52 Post subject: How can I write to a file in an c++ Apache module? |
|
|
Hi all
I have the following code, in C++ (in Ubuntu 16.04) for an Apache HTTP Server module.
I would like to write some debug to logs (using std::ofstream), but I don't see anything. Neither the logs, or the files.
Code: |
#include <httpd.h>
#include <http_log.h>
#include <http_core.h>
#include <http_protocol.h>
#include <http_request.h>
#include <apr_strings.h>
#include <iostream>
#include <fstream>
void write_something_to_a_file(const std::string& path) // <---- DOESN'T WRITE TO FILE!
{
std::ofstream myfile (path);
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
ap_rputs("wrote to file :)\r\n", r);
}
else
{
ap_rputs("Unable to open file\r\n", r);
}
}
static int myserver_handler(request_rec *r)
{
write_something_to_a_file("//home//myfolder//test.txt"); // <-- DOESN'T WORK :(
return OK;
}
static void register_hooks(apr_pool_t *pool)
{
ap_hook_handler(myserver_handler, NULL, NULL, APR_HOOK_LAST);
}
module AP_MODULE_DECLARE_DATA myserver_module =
{
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
NULL,
register_hooks
}; |
The output that I get is always:
Code: | Unable to open file |
What am I missing here?
I'm trying to write to //home//myfolder//test.txt
And I have access to this folder...
Why can't I even create and write to a file?
Thank you for the help! |
|
Back to top |
|
James Blond Moderator
Joined: 19 Jan 2006 Posts: 7371 Location: Germany, Next to Hamburg
|
Posted: Sat 17 Feb '18 11:33 Post subject: |
|
|
Use the internal ap_log_error() function from apache itself. |
|
Back to top |
|
glsmith Moderator
Joined: 16 Oct 2007 Posts: 2268 Location: Sun Diego, USA
|
Posted: Sun 18 Feb '18 0:03 Post subject: |
|
|
I cannot compile your module, tried with vc9 on XP & vc14 on Win10 and I get errors in different <c*> headers
depending on the vc version. gcc must be very forgiving.
That said;
write_something_to_a_file("//home//myfolder//test.txt");
Methinks those should be single forward slashes, not double forward slashes.
You also need the request_rec for ap_rputs, in your module r is undefined in write_something_to_a_file().
Why not just use ANSI C's fopen,fprintf,fputs,fclose since the rest of the module and Apache itself is ANSI C.
This compiles and works for me https://apaste.info/mr0T |
|
Back to top |
|
|
|
|
|
|