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.
I've also tried to use one of the multiple log routines that apache already has on the http_log.h, such as the one below ap_log_pid, but I see nothing being written.
How can I write some debug logs to a file, in an Apache module?
#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() // <---- DOESN'T WRITE TO FILE!
{
std::ofstream myfile;
myfile.open ("//home//myubuntu//myubuntu//textfile_1.txt");
myfile << "Writing this to a file.\n";
myfile.close();
}
static int myserver_handler(request_rec *r)
{
write_something_to_a_file(); // <-- DOESN'T WORK :(
ap_log_pid(p, "//home//myubuntu//myubuntu//textfile_2.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
};
Related
main.cpp
#include "sqlConnection.h"
#include <iostream>
int main() {
sqlConnection *sqlC = new sqlConnection();
sqlC->ifSucceed();
}
sqlConnection.h
#include <Windows.h>
#include <stdio.h>
#include <winsock.h>
#include "mysql.h"
#include <iostream>
#pragma once
using namespace std;
class sqlConnection
{
public:
sqlConnection();
~sqlConnection();
void ifSucceed();
MYSQL mysql;
MYSQL_RES *result;
MYSQL_ROW row;
};
sqlConnection.cpp
#include "sqlConnection.h"
sqlConnection::sqlConnection()
{
mysql_init(&mysql);
mysql_real_connect(&mysql, "xxxxx", "xxxx", "xxxx", "librarySys", 7726, NULL, 0);
}
void sqlConnection::ifSucceed() {
char *sql = "select * from tb_bookcase";
mysql_query(&mysql, sql);
result = mysql_store_result(&mysql);
if ((row = mysql_fetch_row(result)) != NULL) {
cout << "succeed!" << endl;
} else {
cout << "faiiiiiiiiled" << endl;
}
}
sqlConnection::~sqlConnection() {
}
IF there is a libmysql.dll in source file error the message : There
are untreated exception:0x00007FFED00441E6 (libmysql.dll) (in the
librarySys.exe ) 0xC0000005: Access conflict happened when reading
0x0000000000000010.
And then I have to stop. VS give me choice, to change PDB, the binary file path and retry. But I do not know how to do it.
If I delete the libmysql.dll , the error message is:
The program can not be started for losing libmysql.dll in the
computer.Try to reinstall this program to solve this problem.
It's so confusing! I have tried many ways to connect. There are always error messages.
All mysql_* functions return a value that indicates success or failure. Your code disregards them. You ought to confirm whether calls were successful before proceeding to the next call. I surmise you actually failed to connect to the DB and your MYSQL is remains invalid.
Use your debugger. There's no point in coding in the dark. With your debugger you will quickly see whether your MYSQL object has been properly initialized.
Another thing:
sqlConnection *sqlC = new sqlConnection();
There is no reason for this to be heap allocated.
So, I want to put a registry key at the directory HKCU\Software\Microsoft\Windows\CurrentVersion\Run, and I want it to be called Test, and have it contain "TestText", but instead this code puts a new key at HKCU\Test and the program writes random Chinese characters in the registry key. Anyone help?
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <string>
#include <time.h>
using namespace std;
int main()
{
HKEY keyExample;
if (RegOpenKey(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run\\"), &keyExample) != ERROR_SUCCESS)
{
RegCloseKey(keyExample);
return 69;
}
if (RegSetKeyValue(HKEY_CURRENT_USER, TEXT("Test"), 0, REG_SZ, (LPBYTE)"TestText", strlen("TestText")*sizeof(char)) != ERROR_SUCCESS)
{
RegCloseKey(keyExample);
cout << "Unable to set registry value value_name";
}
RegCloseKey(keyExample);
return 0;
}
RegSetKeyValue(HKEY_CURRENT_USER, ...
This is the bug. You need to use the keyExample you got when you opened the key you wanted. Like this:
RegSetKeyValue(keyExample, ...
And for your ANSI/Unicode problem, you need to use the TEXT() macro for your actual data, not just its name:
RegSetKeyValue(keyExample, TEXT("Test"), 0, REG_SZ, TEXT("TestText"), lstrlen(TEXT("TestText"))*sizeof(TCHAR))
It's generally easier to forget all the legacy backward compatible stuff related to the TEXT/TCHAR menus and to directly call the W versions of the Windows API functions with long strings.
I looked around and I couldn't find the answer to how exactly to do this. I am trying to use Pantheios for logging and I want to write to an external file (otherwise whats the point). I am following one of the examples provided but It doesn't seem to be making the log file anywhere. Here is the code:
Edit: Also pantheios_be_file_setFilePath is returning -4 (PANTHEIOS_INIT_RC_UNSPECIFIED_FAILURE) so thats.....not helpful
#include "stdafx.h"
#include <pantheios/pantheios.hpp>
#include <pantheios/implicit_link/core.h>
#include <pantheios/implicit_link/fe.simple.h>
#include <pantheios/implicit_link/be.WindowsConsole.h>
#include <pantheios/implicit_link/be.file.h>
#include <pantheios/frontends/fe.simple.h>
#include <pantheios/backends/bec.file.h>
#include <pantheios/inserters/args.hpp>
PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("LogTest");
int _tmain(int argc, _TCHAR* argv[])
{
try
{
pantheios_be_file_setFilePath(PANTHEIOS_LITERAL_STRING("testlogforme.log"), PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BEID_ALL);
pantheios::log(pantheios::debug, "Entering main(", pantheios::args(argc,argv, pantheios::args::arg0FileOnly), ")");
pantheios::log_DEBUG("debug yo");
pantheios::log_INFORMATIONAL("informational fyi");
pantheios::log_NOTICE("notice me!");
pantheios::log_WARNING("warning!!");
pantheios::log_ERROR("error omg");
pantheios::log_CRITICAL("critical!!!");
pantheios::log_ALERT("alert mang");
pantheios::log_EMERGENCY("EMERGENCY!!!!!");
pantheios_be_file_setFilePath(NULL, PANTHEIOS_BEID_ALL);
system("pause");
return EXIT_SUCCESS;
}
catch(std::bad_alloc&)
{
pantheios::log_ALERT("out of memory");
}
catch(std::exception& x)
{
pantheios::log_CRITICAL("Exception: ", x);
}
catch(...)
{
pantheios::puts(pantheios::emergency, "Unexpected unknown error");
}
return EXIT_FAILURE;
}
Maybe I'm not calling a method or maybe its not being saved to a good location?
It turns out that some of the examples out there for pantheios are incorrect. You DO need to call pantheios_init() even if you are in C++. Here Is the example I got to work after deleting all my code and implementing an example that works.
// Headers for main()
#include <pantheios/pantheios.hpp>
#include <pantheios/backends/bec.file.h>
// Headers for implicit linking
#include <pantheios/implicit_link/core.h>
#include <pantheios/implicit_link/fe.simple.h>
#include <pantheios/implicit_link/be.file.h>
PANTHEIOS_EXTERN_C const char PANTHEIOS_FE_PROCESS_IDENTITY[] = "testLOL";
int main()
{
if(pantheios::pantheios_init() < 0)
{
return 1;
}
pantheios::log_NOTICE("log-1"); // save until log file set
pantheios_be_file_setFilePath("mylogfile.log"); // sets log file; write "log-1" stmt
pantheios::log_NOTICE("log-2"); // write "log-2" stmt
pantheios_be_file_setFilePath(NULL); // close "mylogfile"
pantheios::log_NOTICE("log-3"); // save until log file set
pantheios_be_file_setFilePath("mylogfile2.log"); // sets log file; write "log-3" stmt
pantheios::log_NOTICE("log-4"); // write "log-4" stmt
//system("pause");
return 0;
} // closes "mylogfile2" during program closedown
I found the example on a different post on stack overflow but like I said, the built in examples do not work.
I'm having trouble creating a directory structure in the Windows My Documents directory. I use
ExpandEnvironmentStrings(L"%USERPROFILE%\\Documents",dir,MAX_PATH);
to get the directory then I create a new Directory in there
CreateDirectoryW(dir,NULL)
then in there I want to create another directory so in essence i want Documents\foo\bar however when I go to the foo directory via the Library on explorer side bar 'bar' isn't found unless I go to C:\users\xxx\Documents\foo then its there. Also if I go to Libraries\Documents\foo and right click->New->Folder isn't an option.
I was wondering if there is a Security Option to CreateDirectory I'm supposed to use or what I'm doing wrong
If you want to create a directory tree, you can use SHCreateDirectoryEx. The following code works well on my computer.
#include <iostream>
#include <Windows.h>
#include <Shlobj.h>
#include <Shlwapi.h>
int main()
{
char path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_DOCUMENTS, NULL, 0, path)))
{
PathAppend(path, "foo\\bar");
if (SHCreateDirectoryEx(NULL, path, NULL) != ERROR_SUCCESS)
{
std::cout << "Error: " << GetLastError();
}
}
else
{
std::cout << "Error: " << GetLastError();
}
}
Notice that this works only on Windows 2000 Professional or higher.
I have tried writing a simple program to get information from a website. I can't compile as I get the LNK2019 error for InternetReadFile, InternetOpenUrl, etc. and e.g.
1>GetInternetInfo.obj : error LNK2019: unresolved external symbol _imp_InternetReadFile#16 referenced in function _main
I assume that means I did not define these functions, that I did not include the correct library. I thought including #include would fix it, but it does not seem to help. I am running this on Visual Studio 2010 using C++. Below is my program. Any help is appreciated.
#include <string>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <wininet.h>
#include <winsock.h>
#include <stdio.h>
#include <stdarg.h>
using namespace std;
int main()
{
HINTERNET hOpen, hURL;
LPCWSTR NameProgram = L"Webreader"; // LPCWSTR == Long Pointer to Const Wide String
LPCWSTR Website;
char file[101];
unsigned long read;
//Always need to establish the internet connection with this funcion.
if ( !(hOpen = InternetOpen(NameProgram, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 )))
{
cerr << "Error in opening internet" << endl;
return 0;
}
Website = L"http://www.google.com";
hURL = InternetOpenUrl( hOpen, Website, NULL, 0, 0, 0 ); //Need to open the URL
InternetReadFile(hURL, file, 100, &read);
while (read == 100)
{
InternetReadFile(hURL, file, 100, &read);
file[read] = '\0';
cout << file;
}
cout << endl;
InternetCloseHandle(hURL);
return 0;
}
Please include "Wininet.lib" in your project settings.
Project->Properties->Configuration Properties->Linker->Input->Additional Dependencies
You can also add this line to your code after include section instead of adding library to the properties:
#pragma comment(lib, "wininet.lib")
Did you link to wininet.lib?
http://msdn.microsoft.com/en-us/library/aa385103(VS.85).aspx