logo
Apache Lounge
Webmasters

 


About

Forum Index Downloads Search Register Log in  RSS Apache Lounge
 



Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Apache Lounge is not sponsored.

Your donations will help to keep this site alive and well, and continuing building binaries.



How can I write to a file in an c++ Apache module?

 
Post new topic   Reply to topic    Apache Forum Index -> Apache Building & Member Downloads



View previous topic :: View next topic  
Author Message
joaolourenco82



Joined: 13 Jan 2018
Posts: 1
Location: Portugal, Lisbon

PostPosted: Sun 14 Jan '18 0:52    Post subject: How can I write to a file in an c++ Apache module? Reply with quote

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: 6300
Location: Germany, Next to Hamburg

PostPosted: Sat 17 Feb '18 11:33    Post subject: Reply with quote

Use the internal ap_log_error() function from apache itself.
Back to top
glsmith
Moderator


Joined: 16 Oct 2007
Posts: 2132
Location: Sun Diego, USA

PostPosted: Sun 18 Feb '18 0:03    Post subject: Reply with quote

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


Post new topic   Reply to topic    Apache Forum Index -> Apache Building & Member Downloads
Page 1 of 1