sbErrorConsole.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 #include "sbErrorConsole.h"
28 
29 // Standard includes
30 #include <stdarg.h>
31 
32 // Mozilla includes
33 #include <nsServiceManagerUtils.h>
34 #include <nsStringAPI.h>
35 #include <prprf.h>
36 
37 // Mozilla interfaces
38 #include <nsIConsoleService.h>
39 #include <nsIScriptError.h>
40 
41 class sbConsoleMessage : public nsIConsoleMessage {
42 public:
44  {
45  }
46  sbConsoleMessage(const nsAString & aMessage)
47  {
48  mMessage.Assign(aMessage);
49  }
50 
51  void SetMessage(const nsAString & aMessage)
52  {
53  mMessage = aMessage;
54  }
56  NS_DECL_NSICONSOLEMESSAGE
57 
58 private:
59  ~sbConsoleMessage() {}
60  nsString mMessage;
61 };
62 
64 
65 NS_IMETHODIMP
66 sbConsoleMessage::GetMessageMoz(PRUnichar **result) {
67  *result = ToNewUnicode(mMessage);
68 
69  return NS_OK;
70 }
71 
73 
74 void sbErrorConsole::Error(char const * aCategory,
75  nsAString const & aMessage,
76  nsAString const & aSource,
77  PRUint32 aLine)
78 {
79  nsRefPtr<sbErrorConsole> errorConsole = new sbErrorConsole();
80  if (errorConsole) {
81  errorConsole->Log(nsDependentCString(aCategory),
82  nsIScriptError::errorFlag,
83  aMessage,
84  aSource,
85  aLine);
86  }
87 }
88 
89 void sbErrorConsole::Warning(char const * aCategory,
90  nsAString const & aMessage,
91  nsAString const & aSource,
92  PRUint32 aLine)
93 {
94  nsRefPtr<sbErrorConsole> errorConsole = new sbErrorConsole();
95  if (errorConsole) {
96  errorConsole->Log(nsDependentCString(aCategory),
97  nsIScriptError::warningFlag,
98  aMessage,
99  aSource,
100  aLine);
101  }
102 }
103 
104 void sbErrorConsole::Message(char const * aFmt, ...)
105 {
106  nsRefPtr<sbErrorConsole> errorConsole = new sbErrorConsole();
107  if (errorConsole) {
108  va_list args;
109  va_start(args, aFmt);
110  char *msg = PR_vsmprintf(aFmt, args);
111  errorConsole->Log(nsCString(),
112  infoMessageFlag,
113  NS_ConvertUTF8toUTF16(msg),
114  nsString(),
115  0);
116  PR_smprintf_free(msg);
117  va_end(args);
118  }
119 }
120 
121 nsresult
122 sbErrorConsole::Log(nsACString const & aCategory,
123  PRUint32 aFlags,
124  nsAString const & aMessage,
125  nsAString const & aSource,
126  PRUint32 aLine)
127 {
128  nsresult rv;
129 
130  ErrorParams params(aFlags,
131  aSource,
132  aLine,
133  aMessage,
134  aCategory);
135 
136  if (!NS_IsMainThread()) {
137  // If this fails, not much we can do
138  rv = sbInvokeOnMainThread1(*this,
139  &sbErrorConsole::LogThread,
140  NS_ERROR_FAILURE,
141  params);
142  NS_ENSURE_SUCCESS(rv, rv);
143  }
144  else {
145  rv = LogThread(params);
146  NS_ENSURE_SUCCESS(rv, rv);
147  }
148 
149  return NS_OK;
150 }
151 
152 nsresult sbErrorConsole::LogThread(ErrorParams aParameters)
153 {
154  nsresult rv;
155 
156  nsCOMPtr<nsIConsoleService> consoleService =
157  do_GetService("@mozilla.org/consoleservice;1", &rv);
158  NS_ENSURE_SUCCESS (rv, rv);
159 
160  nsCOMPtr<nsIConsoleMessage> consoleMessage;
161  if (aParameters.mFlags != infoMessageFlag) {
162  nsCOMPtr<nsIScriptError> scriptError =
163  do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
164  if (!scriptError) {
165  return NS_ERROR_FAILURE;
166  }
167 
168  rv = scriptError->Init(aParameters.mMessage.get(),
169  aParameters.mSource.get(),
170  nsString().get(), // no source line
171  aParameters.mLine,
172  0, // No column number
173  aParameters.mFlags,
174  aParameters.mCategory.BeginReading());
175  NS_ENSURE_SUCCESS(rv,rv);
176  consoleMessage = scriptError;
177  }
178  else {
179  consoleMessage = new sbConsoleMessage(aParameters.mMessage);
180  }
181  rv = consoleService->LogMessage(consoleMessage);
182  NS_ENSURE_SUCCESS(rv,rv);
183 
184  return NS_OK;
185 }
NS_IMPL_THREADSAFE_ISUPPORTS0(sbMetadataCrashTracker)
var args
Definition: alert.js:8
return NS_OK
static void Warning(char const *aCategory, nsAString const &aMessage, nsAString const &aSource=nsString(), PRUint32 aLine=0)
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility
static void Message(char const *aFmt,...)
void SetMessage(const nsAString &aMessage)
RT sbInvokeOnMainThread1(T &aObject, MT aMethod, RT aFailureReturnValue, A1 aArg1)
function msg
sbConsoleMessage(const nsAString &aMessage)