Bonjour

actuellement sur mon projet cout et cerr sont redirigées dans des fichiers
je voudrais surcharger un filebuffer afin d'avoir en début de ligne le temps de la trace, parce que cet info n'est pas présente systématiquement.

Voici une ébauche adaptée de codeguru qui permet juste de dumper le flux dans une fenetre debug visual au moment de la synchronisation ou du débordement du buffer, je l'ai un peu modifier pour pouvoir ajouter du texte en début de ligne (pour l'instant pas grand chose, après j'y mettrai l'heure)

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 
class dbgstreambuf : public filebuf
{
public:
	dbgstreambuf(int filed);
	virtual int sync();
	virtual int overflow(int ch = EOF);
protected:
	void sendToDebugWindow();
};
 
 
 
// Allows use of operator << with cout to print to debug window
// in Visual C++. This is a filebuf that echoes its output to the
// specified file and the debug window at the same time. To use it you
// need to attach it to an ostream so for example to attach it to cout
// do the following:
//
// dbgstreambuf dbgbuf(1) // send output to stdout and debug window
// cout = &dbgbuf;
//
// You can then use cout and operator << as usual - i.e cout << "test" << 3 << endl;
 
 
 
 
// Constructor - just create filebuf
dbgstreambuf::dbgstreambuf(int filed) : filebuf(filed)
{
}
 
// This method is called to dump stuff in the put area out to the file.
// We intercept it to send to debug window.
int dbgstreambuf::sync()
{
	sendToDebugWindow();
 
	if (filebuf::sync() == EOF)
		pbump(-out_waiting());
 
	return 0;
}
 
// This method is called to dump stuff in the put area out to the file.
// We intercept it to send to debug window.
int dbgstreambuf::overflow(int ch)
{
	sendToDebugWindow();
 
	if (filebuf::overflow(ch) == EOF)
		pbump(-out_waiting());
 
	return 0;
}
 
// This is where the work happens - We grab a copy of what is in
// the put area and send it to the debug window using OutputDebugString.
void dbgstreambuf::sendToDebugWindow()
{
	// Get pointer to put area
	char *buf = pbase();
	// Number of bytes in put area
	int count = out_waiting();
	if (count) {
 
		// Copy into our own buffer and send to OutputDebugString
		char *dbgstring = new char [8+count + 1];
		memset(dbgstring,'a',8);
		memcpy( dbgstring+8, buf, count );
		dbgstring[count+8] = '\0';
 
		memcpy( buf,dbgstring,  count+8 );
		pbump(8);
		//setp(pbase(),epptr()+8);
		OutputDebugString(dbgstring);
		delete [] dbgstring;
	}
}

bon déjà, que pensez-vous de l'idée?
ensuite, c'est là que je coince, quand et comment fonctionne l'association avec un ofstream?
je crois que je dois créer l'ofstream avec le constructeur par defaut, lui associer le buffer, et enfin ouvrir le fichier? est-ce que là je passe à coté d'un truc?
merci