OpenCBM
libcommon/util.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version
5  * 2 of the License, or (at your option) any later version.
6  *
7  * Copyright 2004 Spiro Trikaliotis
8  *
9  */
10 
19 #include <wdm.h>
20 #include "cbm_driver.h"
21 
45 VOID
46 LogError(IN PDEVICE_OBJECT Fdo,
47  IN NTSTATUS ErrorCode,
48  const WCHAR *String1,
49  const WCHAR *String2)
50 {
51  USHORT stringSize;
52  USHORT stringOffset;
53  USHORT stringSize1;
54  USHORT stringSize2;
55  USHORT size;
56  USHORT numberOfStrings;
57 
58  FUNC_ENTER();
59 
60  // IoAllocateErrorLogEntry() and IoWriteErrorLogEntry() require this
61 
62  DBG_IRQL( <= DISPATCH_LEVEL);
63 
64  // calculate the size of the strings
65 
66  numberOfStrings = 0;
67 
68  // Check if there is a string 1, and calculate its size
69  // (including the trailing zero)
70 
71  stringSize1 = String1 ? (++numberOfStrings, sizeof(WCHAR) * (wcslen(String1) + 1)) : 0;
72 
73  // Check if there is a string 2, and calculate its size
74  // (including the trailing zero)
75 
76  stringSize2 = String2 ? (++numberOfStrings, sizeof(WCHAR) * (wcslen(String2) + 1)) : 0;
77 
78  // Add the sizes of both strings
79  // This is the size of what has to be added to the error log entry
80 
81  stringSize = stringSize1 + stringSize2;
82 
83  // Offset where the string(s) will be written into the error log entry
84 
85  stringOffset = sizeof(IO_ERROR_LOG_PACKET);
86 
87  // The complete size of the event log entry
88 
89  size = stringOffset + stringSize;
90 
91  // Make sure we don't need more space than needed.
92  // For debugging purposes, have an DBG_ASSERT(). Anyway,
93  // in the wild, don't do anything if the size is too big.
94  // Remember: Not being able to write a log is not an error!
95 
100  DBG_ASSERT(size <= ERROR_LOG_MAXIMUM_SIZE);
101 
102  if (size <= ERROR_LOG_MAXIMUM_SIZE)
103  {
104  PIO_ERROR_LOG_PACKET pentry;
105 
106  // allocate the entry for the error log
107 
108  DBG_IRQL( <= DISPATCH_LEVEL);
109  pentry = IoAllocateErrorLogEntry(Fdo, (UCHAR) size);
110 
111  DBG_ASSERT(pentry);
112  if (pentry) {
113 
114  // clear the complete entry (to be sure)
115 
116  RtlZeroMemory(pentry, sizeof(*pentry));
117 
118  // Write the relevant entries
119 
120  pentry->NumberOfStrings = numberOfStrings;
121  pentry->StringOffset = stringOffset;
122  pentry->ErrorCode = ErrorCode;
123 
124  // If there was a string1, write that into the entry
125 
126  if (String1)
127  {
128  wcscpy((wchar_t*)&((UCHAR*)pentry)[stringOffset], String1);
129  }
130 
131  // If there was a string2, write that into the entry
132 
133  if (String2)
134  {
135  wcscpy((wchar_t*)&((UCHAR*)pentry)[stringOffset + stringSize1], String2);
136  }
137 
138  // Now, give that entry to the system
139 
140  DBG_IRQL( <= DISPATCH_LEVEL);
141  IoWriteErrorLogEntry(pentry);
142  }
143  }
144 
145  FUNC_LEAVE();
146 }
#define FUNC_LEAVE()
Definition: debug.h:349
#define DBG_ASSERT(_xxx)
Definition: debug.h:401
#define FUNC_ENTER()
Definition: debug.h:347
Definitions for the opencbm driver.
VOID LogError(IN PDEVICE_OBJECT Fdo, IN NTSTATUS ErrorCode, const WCHAR *String1, const WCHAR *String2)
Log an error entry in the system log.